SpringBoot加載讀取配置文件過程詳細(xì)分析
springboot默認(rèn)讀取的配置文件名字是:“application.properties”和“application.yml”,默認(rèn)讀取四個位置的文件:根目錄下、根目錄的config目錄下、classpath目錄下、classpath目錄里的config目錄下;
配置文件的讀取順序
- 根目錄/config/application.properties
- 根目錄/config/application.yml
- 根目錄/application.properties
- 根目錄/application.yml
- classpath目錄/config/application.properties
- classpath目錄/config/application.yml
- classpath目錄/application.properties
- classpath目錄/application.yml

默認(rèn)可讀取的配置文件全部都會被讀取合并,按照順序讀取配置,相同的配置項(xiàng)按第一次讀取的值為準(zhǔn),同一個目錄下properties文件比yml優(yōu)先讀取,通常會把配置文件放到classpath下,一般是resources里;
多壞境的配置文件
通??梢允褂?個配置文件:(yml也同理)
- application.properties:默認(rèn)配置文件
- application-dev.properties:開發(fā)環(huán)境配置文件
- application-prod.properties:生產(chǎn)環(huán)境配置文件
- application-test.properties:測試環(huán)境配置文件
在application.properties里配置spring.profiles.active以指定使用哪個配置文件,可以配置dev、prod、test分別對應(yīng)以-dev、-prod、-test結(jié)尾的配置文件;(yml配置文件也是同理)
也可以在命令行使用spring.profiles.active指定,例如:java -jarxxxxxx.jar--spring.profiles.active=dev;
個性化配置
對于更特殊的個性化配置可以使用@Profile注解指定;
@Profile標(biāo)簽可以用在@Component或者@Configuration修飾的類上,可以標(biāo)記類和方法,用來指定配置名字,然后使用spring.profiles.active指定該配置名字就可生效;
就像這樣:
package testspringboot.test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("myconfig")
public class MyConfig {
@Bean("Tom")
@Profile("A")
public String a() {
return "tomtom";
}
@Bean("Tom")
@Profile("B")
public String b() {
return "TOMTOM";
}
@Bean("Tom")
public String c() {
return "ttoomm";
}
}然后寫一個controller類:
package testspringboot.test2;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test2controller")
public class Test2Controller {
@Resource(name = "Tom")
public String t;
@RequestMapping("/test2")
public String test2() {
System.out.println(t);
return "TEST2" + t;
}
}啟動類:
package testspringboot.test2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Test2Main {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Test2Main.class, args);
}
}配置文件里配置:
server.port=8888
server.servlet.context-path=/testspringboot
spring.profiles.active=myconfig
只指定myconfig配置,則MyConfig類里c()的bean生效,訪問結(jié)果是:

修改spring.profiles.active=myconfig,A,則MyConfig類里標(biāo)記@Profile("A")的bean生效:

修改spring.profiles.active=myconfig,B,則標(biāo)記@Profile("B")的bean生效:

如果去掉spring.profiles.active配置,則就找不到MyConfig里的配置了,啟動失敗:

自定義配置文件名稱和路徑
可以使用@PropertySource標(biāo)簽指定自定義的配置文件名稱和路徑;(默認(rèn)能加載到的配置文件也會先被加載)

通常只會用到設(shè)置配置文件的名字,并且配置文件的名字可以隨便定義,可以叫xxxx.properties、a.txt、b.abc等等,但是內(nèi)容格式需要跟.properties一致,即kv格式,所以不能直接加載yml格式的配置文件;
@PropertySource默認(rèn)加載路徑是classpath下,可以使用classpath:xxxx/xxxx/xxxx.properties指定目錄和文件,如果使用根目錄則需要使用file:xxxx/xxxx/xxxx.properties;
可以使用@PropertySource為啟動類指定springboot的配置文件,能夠做到使用一個main方法啟動兩個springboot實(shí)例,并各自使用不同的配置文件:
@SpringBootApplication
@PropertySource("classpath:a.properties")
@PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
public class Test2Main {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Test2Main.class, args);
}
}也可以使用@PropertySource配置bean,在使用@Component和@ConfigurationProperties時也可給bean指定特定配置文件:
放在resources下的配置文件tom.abc:
mybean.name=Tom
mybean.age=12
bean類ABC,配置tom.abc文件注入:
package testspringboot.test2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("mybean")
@PropertySource(value = "classpath:tom.abc")
public class ABC {
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "ABC [name=" + name + ", age=" + age + "]";
}
}啟動類可以直接獲得bean:
package testspringboot.test2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:a.properties")
@PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
public class Test2Main {
/**
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Test2Main.class, args);
System.out.println(ctx.getBean(ABC.class));
}
}啟動結(jié)果:

可以直接獲得配置的bean,也可以在代碼里使用@Resource或者@Autowired獲得;
加載yml文件
如果使用@PropertySource配置yml,則需要自定義一個factory實(shí)現(xiàn):
package testspringboot.test2;
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
public class YmlPropertiesFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
factoryBean.setResources(resource.getResource());
factoryBean.afterPropertiesSet();
Properties source = factoryBean.getObject();
return new PropertiesPropertySource("myyml", source);
}
}然后在@PropertySource里配置factory和yml文件:@PropertySource(value = "myapplication.yml", factory = YmlPropertiesFactory.class),就可以加載yml配置文件了;
到此這篇關(guān)于SpringBoot加載讀取配置文件過程詳細(xì)分析的文章就介紹到這了,更多相關(guān)SpringBoot加載配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Java中對List進(jìn)行分區(qū)的實(shí)現(xiàn)方法
在本文中,我們將說明如何將一個列表拆分為多個給定大小的子列表,也就是說在 Java 中如何對List進(jìn)行分區(qū),文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04
Java Netty HTTP服務(wù)實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java Netty HTTP服務(wù)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
使用Java自定義注解實(shí)現(xiàn)一個簡單的令牌桶限流器
限流是在分布式系統(tǒng)中常用的一種策略,它可以有效地控制系統(tǒng)的訪問流量,保證系統(tǒng)的穩(wěn)定性和可靠性,在本文中,我將介紹如何使用Java自定義注解來實(shí)現(xiàn)一個簡單的令牌桶限流器,需要的朋友可以參考下2023-10-10
springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法
這篇文章主要介紹了springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-06-06
詳解基于spring多數(shù)據(jù)源動態(tài)調(diào)用及其事務(wù)處理
本篇文章主要介紹了基于spring多數(shù)據(jù)源動態(tài)調(diào)用及其事務(wù)處理 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

