SpringBoot配置文件加載方法詳細講解
配置文件的讀取順序
- 根目錄/config/application.properties
- 根目錄/config/application.yml
- 根目錄/application.properties
- 根目錄/application.yml
- classpath目錄/config/application.properties
- classpath目錄/config/application.yml
- classpath目錄/application.properties
- classpath目錄/application.yml

默認可讀取的配置文件全部都會被讀取合并,按照順序讀取配置,相同的配置項按第一次讀取的值為準,同一個目錄下properties文件比yml優(yōu)先讀取,通常會把配置文件放到classpath下,一般是resources里;
多壞境的配置文件
通常可以使用4個配置文件:(yml也同理)
- application.properties:默認配置文件
- 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分別對應以-dev、-prod、-test結(jié)尾的配置文件;(yml配置文件也是同理)
也可以在命令行使用spring.profiles.active指定,例如:java -jarxxxxxx.jar--spring.profiles.active=dev;
個性化配置
對于更特殊的個性化配置可以使用@Profile注解指定;
@Profile標簽可以用在@Component或者@Configuration修飾的類上,可以標記類和方法,用來指定配置名字,然后使用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類里標記@Profile("A")的bean生效:

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

如果去掉spring.profiles.active配置,則就找不到MyConfig里的配置了,啟動失?。?/p>

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

通常只會用到設置配置文件的名字,并且配置文件的名字可以隨便定義,可以叫xxxx.properties、a.txt、b.abc等等,但是內(nèi)容格式需要跟.properties一致,即kv格式,所以不能直接加載yml格式的配置文件;
@PropertySource默認加載路徑是classpath下,可以使用classpath:xxxx/xxxx/xxxx.properties指定目錄和文件,如果使用根目錄則需要使用file:xxxx/xxxx/xxxx.properties;
可以使用@PropertySource為啟動類指定springboot的配置文件,能夠做到使用一個main方法啟動兩個springboot實例,并各自使用不同的配置文件:
@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實現(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配置文件了;
到此這篇關于SpringBoot配置文件加載方法詳細講解的文章就介紹到這了,更多相關SpringBoot配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Security代碼實現(xiàn)JWT接口權(quán)限授予與校驗功能
本文給大家介紹Spring Security代碼實現(xiàn)JWT接口權(quán)限授予與校驗功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2019-12-12
解決zuulGateway網(wǎng)關添加路由異常熔斷問題
這篇文章主要介紹了解決zuulGateway網(wǎng)關添加路由異常熔斷問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
IDEA快速部署Spring?Boot?項目到Docker的實現(xiàn)方法
本文主要介紹了IDEA快速部署Spring?Boot?項目到Docker的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Java實現(xiàn)Excel轉(zhuǎn)PDF的兩種方法詳解
使用具將Excel轉(zhuǎn)為PDF的方法有很多,在這里我給大家介紹兩種常用的方法:使用spire轉(zhuǎn)化PDF、使用jacob實現(xiàn)Excel轉(zhuǎn)PDF,分別應對兩種不一樣的使用場景,需要的可以參考一下2022-01-01

