如何访问Spring Boot中的application.properties文件中定义的值
我想访问application.properties提供的值,例如: 
 logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging.file=${HOME}/application.log userBucket.path=${HOME}/bucket 
 我想在Spring Boot应用程序的主程序中访问userBucket.path 。 
 您可以使用@Value注释并访问您正在使用的任何Spring bean的属性 
 @Value("${userBucket.path}") private String userBucketPath; 
Spring Boot文档的“ 外部化configuration”部分解释了您可能需要的所有细节。
另一种方法是注入环境到你的bean。
 @Autowired private Environment env; .... public void method() { ..... String path = env.getProperty("userBucket.path"); ..... } 
  @ConfigurationProperties可用于将.properties (也支持.yml )的值映射到模型类。 
考虑下面的示例文件。
的.properties
 cust.data.employee.name=Sachin cust.data.employee.dept=Cricket 
Employee.java
 import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "cust.data.employee") @Configuration("employeeProperties") public class Employee { private String name; private String dept; //Getters and Setters go here } 
 现在可以通过自动assemblyemployeeProperties来访问属性值,如下所示。 
 @Autowired private Employee employeeProperties; public void method() { String employeeName = employeeProperties.getName(); String employeeDept = employeeProperties.getDept(); }