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: 旺財 age: 3
2、新建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: kkvalue
2、新建工廠轉(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)文章
SSM項(xiàng)目中使用攔截器和過濾器的實(shí)現(xiàn)示例
這篇文章主要介紹了SSM項(xiàng)目中使用攔截器和過濾器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04maven安裝、使用、配置本地倉庫、idea配置maven以及解決plugins報錯問題
本地倉庫是遠(yuǎn)程倉庫的一個緩沖和子集,當(dāng)你構(gòu)建Maven項(xiàng)目時首先會從本地倉庫查找資源,如果沒有那么Maven會從遠(yuǎn)程倉庫下載到你本地倉庫,這篇文章主要給大家介紹了關(guān)于maven安裝、使用、配置本地倉庫、idea配置maven以及解決plugins報錯問題的相關(guān)資料,需要的朋友可以參考下2024-01-01Java中List使用stream流轉(zhuǎn)成map的幾種方式詳解
Stream是Java8中處理集合的關(guān)鍵抽象概念,它可以指定你希望對集合進(jìn)行的操作,可以執(zhí)行非常復(fù)雜的查找、過濾和映射數(shù)據(jù)等操作,下面這篇文章主要給大家介紹了關(guān)于Java中List使用stream流轉(zhuǎn)成map的幾種方式,需要的朋友可以參考下2023-04-04