2 回答
TA贡献1770条经验 获得超3个赞
您是否考虑过使用 Apache 的commons-configuration?
要添加的依赖项pom.xml
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
你的课会像
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class Configurations {
private CompositeConfiguration config;
public void getConstants() throws ConfigurationException {
config = new CompositeConfiguration();
config.addConfiguration(new PropertiesConfiguration("test.properties"));
}
public CompositeConfiguration getConfig() {
return config;
}
public static void main(String... args) throws ConfigurationException {
Configurations config = new Configurations();
config.getConstants();
System.out.println(config.getConfig().getString("SOURCE"));
System.out.println(config.getConfig().getString("SOURCES"));
}
}
测试属性
SOURCE=Source
SOURCES=${SOURCE}s
DESTINATION=Destination
DESTINATIONS=${DESTINATION}s
TA贡献1895条经验 获得超7个赞
您可以在属性文件中使用“%s”进行字符串组合:
SOURCE=Source SOURCES=%ss
然后,您必须格式化字符串:
public static String SOURCES = String.format(prop.getProperty("SOURCES"), prop.getProperty("SOURCE"));
添加回答
举报