av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

瀏覽:4日期: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
相關文章:
主站蜘蛛池模板: 欧美日韩一区精品 | 久久一区二区av | av高清毛片| 99一级毛片 | 涩涩视频网站在线观看 | 精品福利av导航 | 欧美极品视频在线观看 | 亚洲福利一区 | 在线一区 | 国产精品一区久久久 | 国产精品日日摸夜夜添夜夜av | 在线日韩 | 亚洲 精品 综合 精品 自拍 | 欧美精品久久久久久久久老牛影院 | 日韩国产欧美一区 | 久久99精品国产自在现线小黄鸭 | 我想看国产一级毛片 | 国产欧美一级 | 一级特黄色毛片 | 欧美中文在线 | 久久久久久久久久久高潮一区二区 | 国产精品人人做人人爽 | 亚洲人在线观看视频 | 国产一级片精品 | 影音先锋中文字幕在线观看 | 精品伊人 | 成人精品一区亚洲午夜久久久 | www精品| 97国产精品视频人人做人人爱 | 久久久91精品国产一区二区精品 | 日韩中文字幕在线观看 | 午夜电影网站 | 国产激情视频 | 欧美一级黄色片在线观看 | 亚洲成人久久久 | 99亚洲精品视频 | 国产免费拔擦拔擦8x高清 | 国产精品日韩一区二区 | 精品一二三 | 日韩视频在线观看一区二区 | 日本a在线|