硬编码 java_如何避免Java中的硬编码

硬编码 java_如何避免Java中的硬编码你必须 severals 事情尝试 Java 属性 privatePrope properties privatevoidi properties newPropertie InputStreamc PropertiesCo class getClassLoad getResou

你必须severals事情尝试:

Java属性

private Properties _properties;

private void init(){

_properties = new Properties();

InputStream configurationFileIS = PropertiesConfigurationHandler.class.getClassLoader().getResourceAsStream(CONFIGURATION_FILE);

_properties.load(configurationFileIS);

}

public String getStringValue(String path) {

return _properties.getProperty(path);

}

和属性文件将类似于

an.element.to.be.configured.like.a.path=/dev/null

但你也可以使用Spring上下文

class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>

WEB-INF/classes/config/properties/database.properties

classpath:config/properties/database.properties

和内部database.properties的元素将被访问的方式

“${jdbc.username}”

为了您的具体问题。

您可以创建一个文件constants.properties

executable.run=C:\\server\\rd.exe

incoming.file=C:\\file.lic

executable.params=-z

log.file=C:\\File\\log.txt

,然后你调用向GetStringValue初始化后:

String executable = getStringValue(“executable.run”);

String filein = getStringValue(“incoming.file”);

String params = getStringValue(“executable.params”);

String log = getStringValue(“log.file”);

然后,你可以做和的rt.exec而不是使用硬编码字符串,你可以使用之前检索到的。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/177365.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午7:41
下一篇 2026年3月26日 下午7:41


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号