SpringBoot读取application.properties文件

SpringBoot读取application.properties文件,通常有3种方式

1. @Value  例如: 

@Value("${spring.profiles.active}")

private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中

2. @ConfigurationProperties  例如:

@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;

}

其他类中使用时,就可以直接注入该TestProperties 进行访问相关的值

3. 使用Enviroment   例如:

private Enviroment env;

env.getProperty("test.url");

而env方式效率较低

注:@ConfigurationProperties也可用于其他.properties文件,只要locations指定即可

 
 
原文地址:https://www.cnblogs.com/haore147/p/7212582.html