硬编码 java_如何避免在java中进行硬编码

你有几件事要尝试:

java PROPERTIES

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 CONTEXT

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

然后在init之后调用getStringValue:

String executable = getStringValue("executable.run");

String filein = getStringValue("incoming.file");

String params = getStringValue("executable.params");

String log = getStringValue("log.file");

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值