Spring — 项目中@ConfigurationProperties 注解的使用Demo
今天翻看项目的代码,发现一个有意思的注解:@ConfigurationProperties,直接上代码:
application.yml配置:
msg:
test-http1: http://127.0.0.1:10000/
test-http2: http://127.0.0.1:10000/
test-http3: http://127.0.0.1:10000/
test-http4: http://127.0.0.1:10000/
@Service
@ConfigurationProperties(prefix = "msg")
public class DownlinkServiceImpl {
private HashMap<String, String> pool = new HashMap<>();
public HashMap<String, String> getPool() {
return pool;
}
System.out.println("sgi are " + pool );
}
println:
sgi are {test-http1=http://127.0.0.1:10000/, test-http2=http://127.0.0.1:10000/, test-http3=http://127.0.0.1:10000/, test-http4=http://127.0.0.1:10000/}
这样就可以就可以直接把配置文件中的值,以hashmap的方式存起来,取的时候直接get就可以,还是很方便的;