SpringBoot读取application.properti

2017-02-11  本文已影响619人  Superwind20

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

1.@Value例如:

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

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

2.@ConfigurationProperties例如:

@Component

@ConfigurationProperties(locations = "classpath:test.properties",prefix="test")

public class TestProperties {

String url;

String key;

}

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

3.使用Enviroment例如:

private Enviroment env;

env.getProperty("test.url");

而env方式效率较低

注:@ConfigurationProperties也可用于其他.properties文件,只要locations指定即可。(locations在springboot 1.5版后已去掉了,可将配置移到application.properties或application-{profile}.properties中)

上一篇 下一篇

猜你喜欢

热点阅读