【发布时间】:2017-11-13 22:07:48
【问题描述】:
请你帮我从 Spring Boot 中的 application.properties 文件中读取属性,而不自动装配 Environment 并且不使用 Environment?
也不需要使用${propname}。我可以创建属性对象,但必须传递我的属性文件路径。我想从其他位置获取我的道具文件。
【问题讨论】:
标签: java spring spring-boot
请你帮我从 Spring Boot 中的 application.properties 文件中读取属性,而不自动装配 Environment 并且不使用 Environment?
也不需要使用${propname}。我可以创建属性对象,但必须传递我的属性文件路径。我想从其他位置获取我的道具文件。
【问题讨论】:
标签: java spring spring-boot
OrangeDog 解决方案对我不起作用。它产生了 NullPointerException。
我找到了另一个解决方案:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try (InputStream resourceStream = loader.getResourceAsStream("application.properties")) {
properties.load(resourceStream);
} catch (IOException e) {
e.printStackTrace();
}
【讨论】:
这是一个核心 Java 功能。如果您不想使用,则不必使用任何 Spring 或 Spring Boot 功能。
Properties properties = new Properties();
try (InputStream is = getClass().getResourceAsStream("application.properties")) {
properties.load(is);
}
JavaDoc:http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html
【讨论】:
尝试使用普通的旧Properties。
final Properties properties = new Properties();
properties.load(new FileInputStream("/path/config.properties"));
System.out.println(properties.getProperty("server.port"));
如果您需要在配置中使用该外部属性文件,可以使用@PropertySource("/path/config.properties") 来完成
【讨论】:
以下代码从现有的 application.properties 文件中提取环境值,该文件位于 WEB-INF/classes 下的已部署资源中:
// Define classes path from application.properties :
String environment;
InputStream inputStream;
try {
// Class path is found under WEB-INF/classes
Properties prop = new Properties();
String propFileName = "com/example/project/application.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
// read the file
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
// get the property value and print it out
environment = prop.getProperty("environment");
System.out.println("The environment is " + environment);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
这里是示例,使用来自 application.properties(文本文件)的以下输入运行上述代码:
# Application settings file
environment=Test
release_date=DATE
session_timeout_minutes=25
## Allowable image types
img_file_extensions="jpeg;pjpeg;jpg;png;gif"
## Images are saved with this extension
img_default_extension=jpg
# Mail Settings / Addresses
mail_debug=false
Output:
The environment is Test
【讨论】:
要阅读 application.properties,只需将此注释添加到您的类中:
@ConfigurationProperties
public class Foo {
}
如果要更改默认文件
@PropertySource("your properties path here")
public class Foo {
}
【讨论】:
如果其他所有设置都正确,您可以注释@Value。 Springboot 将负责从属性文件中加载值。
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.beans.factory.annotation.Value;
@Configuration
@PropertySource("classpath:/other.properties")
public class ClassName {
@Value("${key.name}")
private String name;
}
【讨论】:
除了 Vladislav Kysliy 的优雅解决方案之外,下面的代码可以直接作为 REST API 调用插入,以在不知道任何键的情况下获取 Spring Boot 中 application.properties 文件的所有键/值。此外,如果您知道 Key,则可以随时使用 @Value 注释来查找值。
@GetMapping
@RequestMapping("/env")
public java.util.Set<Map.Entry<Object,Object>> getAppPropFileContent(){
ClassLoader loader = Thread.currentThread().getContextClassLoader();
java.util.Properties properties = new java.util.Properties();
try(InputStream resourceStream = loader.getResourceAsStream("application.properties")){
properties.load(resourceStream);
}catch(IOException e){
e.printStackTrace();
}
return properties.entrySet();
}
【讨论】: