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指定即可

 
 
posted @ 2017-07-20 17:54  二郎那个三郎  阅读(5800)  评论(0编辑  收藏  举报