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

标签 java

我已经阅读了很多关于避免在 Java 中进行硬编码的文章。但是无法清楚地了解如何将其应用于我的要求。在做了一些研究之后我问了这个问题。下面是我的代码片段。因为我想避免在 Process pr = rt.exec() 中对路径名进行硬编码。关于如何做到这一点有什么建议吗?

public class StartUp {

String executable = getStringValue("executable.run");
    String filein = getStringValue("incoming.file");
    String params1 = getStringValue("executable.params1");
    String params2 = getStringValue("executable.params2");
    String log = getStringValue("log.file");
String ss = "Started";
public String startCommand() throws IOException, InterruptedException{



Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("C:\\server\\rd.exe -a C:\\file.lic -z[+] // C:\\File\\log.txt");

Process pr = rt.exec(executable+" "+params1+" "+filein+" "+params2+" "+log);
    BufferedReader input = new BufferedReader(new InputStreamReader 
(pr.getInputStream()));

String line=null;
StringBuffer start= new StringBuffer();
    while((line=input.readLine()) != null) {
                start.append("ServerStarted" + line + "\n");
        System.out.println(line);
}

int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
return line;



//return start.toString();
}
private static String getStringValue(String string) {
    return string;

}
}

最佳答案

你有几件事要尝试:

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 CONTEXT

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>WEB-INF/classes/config/properties/database.properties</value>
            <value>classpath:config/properties/database.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

并且将以这种方式访问​​ 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,而不是使用硬编码字符串,您可以使用之前检索到的字符串。

关于java - 如何避免在 Java 中进行硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251324/

相关文章:

java - 为什么 'java -version' 会转到 stderr?

java - lucene/Solr 性能和硬件要求

java - header 值在 HTTPClient 中重定向时被覆盖

java - 我可以对整个搜索应用程序使用单个 Lucene Analyzer 实例吗?

java - 错误;无法找到或加载主类(使用 Windows CMD 的 Java)

java - Collections.max(array) 不工作 - Java

java - 抛出异常的哪一部分是昂贵的?

java - 来自 BufferedReader 的 Scanner.next()

java - netty中ChannelBuffer.copy()和ChannelBuffer.duplicate()有什么区别

java - 字符串替换最后的所有特殊字符