詳解Spring Boot加載properties和yml配置文件
一、系統(tǒng)啟動(dòng)后注入配置
package com.example.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; /** * @author: GrandKai * @create: 2016-09-01 11:24 */ @Configuration @PropertySource(ignoreResourceNotFound = true, value = {"classpath:/config/email.properties","classpath:/config/email.yml"}, name = "email") public class Config {}
需要在ApplicationContext中注冊(cè)配置
AnnotationConfigEmbeddedWebApplicationContext context = (AnnotationConfigEmbeddedWebApplicationContext) app.run("參數(shù)1"); context.register(Config.class);
用以下方式取值
Environment env = context.getEnvironment(); System.out.println(env.getProperty("address"));
email.yml文件配置如下:
server: address: 127.0.0.1
二、在命令行傳入注入到程序中
public class Main { public static void main(String... args) { //initialize the command line parsing stuff OptionParser parser = new OptionParser(); parser.accepts("greeting").withRequiredArg(); OptionSet options = parser.parse(args); //create the actual Spring PropertySource PropertySource<?> ps = new JOptCommandLinePropertySource(options); //setup the Spring context AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().getPropertySources().addLast(ps); //register the property source with the environment ctx.register(Greeter.class); ctx.refresh(); Greeter greeter = ctx.getBean(Greeter.class); greeter.sayGreeting(); } } @Component class Greeter { @Inject private Environment env; //the following would also work //@Value("${greeting}") //private String greeting; /** * Print out the 'greeting' property if it exists, and otherwise, "Welcome!". */ public void sayGreeting() { System.out.println(env.getProperty("greeting", "Welcome!")); } } public static void main(String [] args) { SimpleCommandLinePropertySource ps = new SimpleCommandLinePropertySource(args); @SuppressWarnings("resource") AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().getPropertySources().addFirst(ps); ctx.register(ApplicationConfig.class); ctx.refresh(); } @Configuration @EnableScheduling @ComponentScan("com.mycompany.package") @PropertySource( value = {"classpath:/application.properties", "file:${config.location}"}, ignoreResourceNotFound = true ) class ApplicationConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } @Component class MyComponent { @Value("${my.property.data}") private String myPropertyData; @Scheduled(fixedDelayString = "${schedule.delay.period}") public void run() { : } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè)
這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)輪廓檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器
這篇文章主要介紹了jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10使用java + OpenCV破解頂象面積驗(yàn)證碼的示例
這篇文章主要介紹了使用java + OpenCV破解頂象面積驗(yàn)證碼的示例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀
這篇文章主要介紹了Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀,在Java早期版本中,synchronized 屬于 重量級(jí)鎖,效率低下,這是因?yàn)楸O(jiān)視器鎖(monitor)是依賴(lài)于底層的操作系統(tǒng)的Mutex Lock來(lái)實(shí)現(xiàn)的,Java 的線程是映射到操作系統(tǒng)的原生線程之上的,需要的朋友可以參考下2023-12-12java核心編程之文件過(guò)濾類(lèi)FileFilter和FilenameFilter
這篇文章主要為大家詳細(xì)介紹了java文件過(guò)濾類(lèi)FileFilter和FilenameFilter,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08