詳解Spring Boot加載properties和yml配置文件
更新時間:2017年04月13日 09:52:16 作者:賽亞人之神
本篇文章主要介紹了詳解Spring Boot加載properties和yml配置文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
一、系統(tǒ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中注冊配置
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() { : } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別
下面小編就為大家?guī)硪黄獪\談靜態(tài)變量、成員變量、局部變量三者的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器
這篇文章主要介紹了jackson在springboot中的使用方式-自定義參數(shù)轉(zhuǎn)換器,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀
這篇文章主要介紹了Java并發(fā)編程中的synchronized關(guān)鍵字詳細(xì)解讀,在Java早期版本中,synchronized 屬于 重量級鎖,效率低下,這是因為監(jiān)視器鎖(monitor)是依賴于底層的操作系統(tǒng)的Mutex Lock來實現(xiàn)的,Java 的線程是映射到操作系統(tǒng)的原生線程之上的,需要的朋友可以參考下2023-12-12java核心編程之文件過濾類FileFilter和FilenameFilter
這篇文章主要為大家詳細(xì)介紹了java文件過濾類FileFilter和FilenameFilter,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08