亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring外部化配置的幾種技巧分享

瀏覽:2日期:2023-07-08 13:02:09
目錄正文Envrionment 獲取外部配置修改Spring默認配置文件名稱Value注解配置來源外部化配置文件優先級問題Autowire注入ConfigurableEnvrionmentApplicationInitialiazer 配置總結正文Envrionment 獲取外部配置

@Log4j2@SpringBootApplicationpublic class ConfigurationApplication { public static void main(String[] args) {SpringApplication.run(ConfigurationApplication.class, args); } @Bean ApplicationRunner applicationRunner(Environment environment){return args -> { log.info('user.name : {}',environment.getProperty('user.name'));}; }}修改Spring默認配置文件名稱

啟動程序參數中加入如下配置:

--spring.config.name=appValue注解配置來源

配置文件

@BeanApplicationRunner applicationRunner(Environment environment, @Value('${greeting.message:hello boy}') String message){ return args -> { log.info('from application.properties user.name : {}',environment.getProperty('user.name')); log.info('from application.properties greeting.message : {}',message); };}

默認值

value注解通過冒號來配置默認值:

@Value('${greeting.message:hello boy}')

獲取環境變量值

獲取程序參數值

外部化配置文件優先級問題

如果有application.properties在springboot 啟動jar包同一目錄,會優先讀取這個文件中的配置。

Autowire注入ConfigurableEnvrionment

public static void main(String[] args) {new SpringApplicationBuilder().sources(ConfigurationApplication.class).run(args);}@Autowiredvoid getConfigurableEnvrionment(ConfigurableEnvironment environment) { environment.getPropertySources().addLast(new MyPropertySource());}ApplicationInitialiazer 配置

public static void main(String[] args) {new SpringApplicationBuilder().sources(ConfigurationApplication.class).initializers(applicationContext -> applicationContext.getEnvironment().getPropertySources().addLast(new MyPropertySource())).run(args); }static class MyPropertySource extends PropertySource<String>{ public MyPropertySource() { super('myproperty'); } @Override public Object getProperty(String name) { if(name.equalsIgnoreCase('author-name')){ return 'john'; } return null; }}

然后通過@Value注解注入獲取author-name:

@Bean ApplicationRunner applicationRunner(Environment environment,@Value('${greeting.message:hello boy}') String message,@Value('${author-name}') String name){return args -> { log.info('from application.properties user.name : {}',environment.getProperty('user.name')); log.info('from application.properties author.name : {}',name);}; }總結

Spring的Environment抽象有很多值得學習的地方,期待下一期每日小技巧。

以上就是Spring外部化配置的幾種技巧分享的詳細內容,更多關于Spring外部化配置的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 免费看欧美一级特黄α大片 | 国产精品嫩草影院在线播放 | 婷婷视频在线 | 国产欧美一区二区三区视频在线观看 | 特级黄色录像片 | 国产高清一区二区三区四区 | 俄罗斯女人与公拘i交酡 | 亚洲国产高清在线 | 亚洲一区欧美一区 | 五月天爱爱视频 | 国产一区二区三区在线观看精品 | 日本做爰免费大片视频 | 在线视频一区二区三区四区 | 麻豆精品视频网站在线观看 | 亚洲欧美日韩第一页 | 男女很黄很色床视频网站免 | 成人综合久久综合 | 又爽又刺激的欧美毛片 | 国产a视频精品免费观看 | 精品国产日韩亚洲一区91 | 国产午夜精品视频 | 久久精品视频在线播放 | 国产区精品高清在线观看 | 成人精品免费视频 | 免费一级在线观看 | 国产婷婷色综合成人精品 | 国产精品嫩草影院午夜 | 色屁屁www影院入口免费观看 | 国产在线精品观看一区 | 九九视频免费观看 | 成人午夜精品久久久久久久小说 | 国产成人黄网在线免 | 在线观看免费国产视频 | 国产一区二区三区免费播放 | 国产成人精品免费视频大全麻豆 | 小明成人永久视频在线观看 | 亚洲国产欧美日韩精品小说 | 免费一级特黄欧美大片勹久久网 | 成年人性网站 | 中文字幕欧美日韩 | 久久久国产精品福利免费 |