SpringBoot讀取自定義配置文件方式(properties,yaml)
一、讀取系統(tǒng)配置文件application.yaml
1、application.yaml配置文件中增加一下測試配置
testdata:
animal:
lastName: 動物
age: 18
boss: true
birth: 2022/02/22
maps: {key1:value1,key2:value2}
list: [dog,cat,house]
dog:
name: 旺財(cái)
age: 32、新建entity實(shí)體類Animal
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component//標(biāo)識為Bean
@ConfigurationProperties(prefix = "testdata.animal")//prefix前綴需要和yml配置文件里的匹配。
@Data//這個是一個lombok注解,用于生成getter&setter方法
public class Animal {
private String lastName;
private int age;
private boolean boss;
private Date birth;
private Map<String,String> maps;
private List<String> list;
private Dog dog;
}3、新建entity實(shí)體類dog
package com.example.demo.db.config;
import lombok.Data;
import org.springframework.stereotype.Component;
@Component//標(biāo)識為Bean
@Data//這個是一個lombok注解,用于生成getter&setter方法
@Configuration//標(biāo)識是一個配置類
public class Dog {
private String name;
private int age;
}4、新建測試類MyTest
import com.example.demo.db.config.RemoteProperties;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)//讓測試運(yùn)行于Spring測試環(huán)境
@SpringBootTest(classes = DemoApplication.class)//指定啟動類
@EnableConfigurationProperties(RemoteProperties.class)//使RemoteProperties注解類生效
public class MyTests {
@Autowired
Animal animal;
@Test
public void test2() {
System.out.println("person===="+animal);
System.out.println("age======="+animal.getAge());
System.out.println("dog.name======="+animal.getDog().getName()+" dog.age===="+animal.getDog().getAge());
}
}5、運(yùn)行結(jié)果:

二、讀取自定義配置文件properties格式內(nèi)容
1、resources\config目錄下新建remote.properties配置文件,內(nèi)容如下:
remote.testname=張三 remote.testpass=123456 remote.testvalue=ceshishuju
2、新建entity實(shí)體類RemoteProperties
package com.example.demo.db.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration//表明這是一個配置類
@ConfigurationProperties(prefix = "remote",ignoreInvalidFields = false)//該注解用于綁定屬性。prefix用來選擇屬性的前綴,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用來告訴SpringBoot在有屬性不能匹配到聲明的域時拋出異常。
@PropertySource(value="classpath:config/remote.properties",ignoreResourceNotFound = false)//配置文件路徑
@Data//這個是一個lombok注解,用于生成getter&setter方法
@Component//標(biāo)識為Bean
public class RemoteProperties {
private String testname;
private String testpass;
private String testvalue;
}3、新建測試類MyTests
import com.example.demo.db.config.RemoteProperties;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)//讓測試運(yùn)行于Spring測試環(huán)境
@SpringBootTest(classes = DemoApplication.class)//指定啟動類
@EnableConfigurationProperties(RemoteProperties.class)//應(yīng)用配置文件類,使RemoteProperties注解類生效
public class MyTests {
@Autowired
RemoteProperties remoteProperties;
@Test
public void test2() {
TestCase.assertEquals(1, 1);
String testpass=remoteProperties.getTestpass();
System.out.println("-----------:"+testpass);
System.out.println("------------:"+remoteProperties.getTestvalue());
}
}4、運(yùn)行結(jié)果:

三、讀取自定義配置文件yaml格式內(nèi)容
1、resources\config目錄下新建remote.yaml配置文件,內(nèi)容如下:
remote:
person:
testname: 張三
testpass: 123456
testvalue: kkvalue2、新建工廠轉(zhuǎn)換類PropertySourceFactory
package com.example.demo.db.config;
import org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
//把自定義配置文件.yml的讀取方式變成跟application.yml的讀取方式一致的 xx.xx.xx
public class MyPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {
return new YamlPropertySourceLoader().load(name,encodedResource.getResource()).get(0);
}
}3、新建entity實(shí)體類RemoteProperties
package com.example.demo.db.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration//表明這是一個配置類
@ConfigurationProperties(prefix = "remote",ignoreInvalidFields = false)//該注解用于綁定屬性。prefix用來選擇屬性的前綴,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用來告訴SpringBoot在有屬性不能匹配到聲明的域時拋出異常。
@PropertySource(value="classpath:config/remote.yaml",factory = MyPropertySourceFactory.class)//配置文件路徑,配置轉(zhuǎn)換類
@Data//這個是一個lombok注解,用于生成getter&setter方法
@Component//標(biāo)識為Bean
public class RemoteProperties {
@Value("${remote.person.testname}")//根據(jù)配置文件寫全路徑
private String testname;
@Value("${remote.person.testpass}")
private String testpass;
@Value("${remote.person.testvalue}")
private String testvalue;
}4、新建測試類MyTests
import com.example.demo.db.config.RemoteProperties;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)//讓測試運(yùn)行于Spring測試環(huán)境
@SpringBootTest(classes = DemoApplication.class)//指定啟動類
@EnableConfigurationProperties(RemoteProperties.class)//使RemoteProperties注解類生效
public class MyTests {
@Autowired
RemoteProperties remoteProperties;
@Test
public void test2() {
TestCase.assertEquals(1, 1);
String testpass=remoteProperties.getTestpass();
System.out.println("asdfasdf"+testpass);
System.out.println("asdfasdf"+remoteProperties.getTestvalue());
}
}5、運(yùn)行結(jié)果:

說明:
![]()
這里需要寫一個工廠去讀取propertySource(在調(diào)試的時候我看到默認(rèn)讀取的方式是xx.xx.xx而自定義的yml配置文件是每一個xx都是分開的,所以不能獲取到,而自己創(chuàng)建的配置類MyPropertySourceFactory就是需要把自定義配置文件.yml的讀取方式變成跟application的讀取方式一致的 xx.xx.xx,并且通過@Value注解指定變量的的關(guān)系和yaml配置文件對應(yīng))
四、其他擴(kuò)展內(nèi)容
可以加入依賴spring-boot-configuration-processor后續(xù)寫配置文件就有提示信息:
<!-- 導(dǎo)入文件處理器,加上這個,以后編寫配置就有提示了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor</artifactId>
<optional> true </optional>
</dependency>其他獲取配置相關(guān)內(nèi)容后續(xù)更新。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 語言守護(hù)線程 Daemon Thread使用示例詳解
這篇文章主要為大家介紹了Java 語言守護(hù)線程 Daemon Thread使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
SpringBoot請求參數(shù)相關(guān)注解說明小結(jié)
這篇文章主要介紹了SpringBoot請求參數(shù)相關(guān)注解說明,主要包括@PathVariable,@RequestHeader、@CookieValue、@RequestBody和@RequestParam,本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-05-05
Java如何在 Word 中設(shè)置上、下標(biāo)
這篇文章主要介紹了Java如何在 Word 中設(shè)置上、下標(biāo),幫助大家更好的利用Java處理文檔,感興趣的朋友可以了解下2020-09-09
解析springboot集成AOP實(shí)現(xiàn)日志輸出的方法
如果這需要在每一個controller層去寫的話代碼過于重復(fù),于是就使用AOP定義切面 對其接口調(diào)用前后進(jìn)行攔截日志輸出。接下來通過本文給大家介紹springboot集成AOP實(shí)現(xiàn)日志輸出,需要的朋友可以參考下2021-11-11
springboot集成Mybatis-plus-join-boot-start詳解
這篇文章主要介紹了springboot集成Mybatis-plus-join-boot-start方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Java經(jīng)典算法匯總之選擇排序(SelectionSort)
選擇排序也是比較簡單的一種排序方法,原理也比較容易理解,選擇排序在每次遍歷過程中只記錄下來最小的一個元素的下標(biāo),待全部比較結(jié)束之后,將最小的元素與未排序的那部分序列的最前面一個元素交換,這樣就降低了交換的次數(shù),提高了排序效率。2016-04-04

