我通过Spring Boot文档了解了externalized configuration,我发现它会自动加载src/main/resources/application.properties文件,然后可以将它连接到使用注释的bean属性。将application.properties文件加载到Spring引导中的java.util.Properties中
但是我想要一个通用的PropertyHelper
类,它可以用来构建具有application.properties中属性的java.util.Properties
。这可以做到吗?
目前,我们正在实现这一手动如下:
public class PropertyHelper {
private static Properties loadProperties() {
try {
String propsName = "application.properties";
InputStream propsStream = PropertyHelper.class
.getClassLoader().getResourceAsStream(propsName);
if (propsStream == null) {
throw new IOException("Could not read config properties");
}
Properties props = new Properties();
props.load(propsStream);
之前'添加斜线application.properties' – Jens
或者你可以自动装配环境,这是一个包含所有值的属性型豆从文件 – rorschach
使用'Environment'您可以获取属性,但它没有所有属性的列表。你只能使用'env.getProperty(“propertyName”)'获得属性 –