SpringBoot讀取自定義配置文件方式(properties,yaml)
一、讀取系統(tǒng)配置文件application.yaml
1、application.yaml配置文件中增加一下測(cè)試配置
testdata: animal: lastName: 動(dòng)物 age: 18 boss: true birth: 2022/02/22 maps: {key1:value1,key2:value2} list: [dog,cat,house] dog: name: 旺財(cái) age: 3
2、新建entity實(shí)體類(lèi)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)識(shí)為Bean @ConfigurationProperties(prefix = "testdata.animal")//prefix前綴需要和yml配置文件里的匹配。 @Data//這個(gè)是一個(gè)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í)體類(lèi)dog
package com.example.demo.db.config; import lombok.Data; import org.springframework.stereotype.Component; @Component//標(biāo)識(shí)為Bean @Data//這個(gè)是一個(gè)lombok注解,用于生成getter&setter方法 @Configuration//標(biāo)識(shí)是一個(gè)配置類(lèi) public class Dog { private String name; private int age; }
4、新建測(cè)試類(lèi)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)//讓測(cè)試運(yùn)行于Spring測(cè)試環(huán)境 @SpringBootTest(classes = DemoApplication.class)//指定啟動(dòng)類(lèi) @EnableConfigurationProperties(RemoteProperties.class)//使RemoteProperties注解類(lèi)生效 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í)體類(lèi)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//表明這是一個(gè)配置類(lèi) @ConfigurationProperties(prefix = "remote",ignoreInvalidFields = false)//該注解用于綁定屬性。prefix用來(lái)選擇屬性的前綴,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用來(lái)告訴SpringBoot在有屬性不能匹配到聲明的域時(shí)拋出異常。 @PropertySource(value="classpath:config/remote.properties",ignoreResourceNotFound = false)//配置文件路徑 @Data//這個(gè)是一個(gè)lombok注解,用于生成getter&setter方法 @Component//標(biāo)識(shí)為Bean public class RemoteProperties { private String testname; private String testpass; private String testvalue; }
3、新建測(cè)試類(lèi)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)//讓測(cè)試運(yùn)行于Spring測(cè)試環(huán)境 @SpringBootTest(classes = DemoApplication.class)//指定啟動(dòng)類(lèi) @EnableConfigurationProperties(RemoteProperties.class)//應(yīng)用配置文件類(lèi),使RemoteProperties注解類(lèi)生效 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)換類(lèi)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í)體類(lèi)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//表明這是一個(gè)配置類(lèi) @ConfigurationProperties(prefix = "remote",ignoreInvalidFields = false)//該注解用于綁定屬性。prefix用來(lái)選擇屬性的前綴,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用來(lái)告訴SpringBoot在有屬性不能匹配到聲明的域時(shí)拋出異常。 @PropertySource(value="classpath:config/remote.yaml",factory = MyPropertySourceFactory.class)//配置文件路徑,配置轉(zhuǎn)換類(lèi) @Data//這個(gè)是一個(gè)lombok注解,用于生成getter&setter方法 @Component//標(biāo)識(shí)為Bean public class RemoteProperties { @Value("${remote.person.testname}")//根據(jù)配置文件寫(xiě)全路徑 private String testname; @Value("${remote.person.testpass}") private String testpass; @Value("${remote.person.testvalue}") private String testvalue; }
4、新建測(cè)試類(lèi)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)//讓測(cè)試運(yùn)行于Spring測(cè)試環(huán)境 @SpringBootTest(classes = DemoApplication.class)//指定啟動(dòng)類(lèi) @EnableConfigurationProperties(RemoteProperties.class)//使RemoteProperties注解類(lèi)生效 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é)果:
說(shuō)明:
這里需要寫(xiě)一個(gè)工廠去讀取propertySource(在調(diào)試的時(shí)候我看到默認(rèn)讀取的方式是xx.xx.xx而自定義的yml配置文件是每一個(gè)xx都是分開(kāi)的,所以不能獲取到,而自己創(chuàng)建的配置類(lèi)MyPropertySourceFactory就是需要把自定義配置文件.yml的讀取方式變成跟application的讀取方式一致的 xx.xx.xx,并且通過(guò)@Value注解指定變量的的關(guān)系和yaml配置文件對(duì)應(yīng))
四、其他擴(kuò)展內(nèi)容
可以加入依賴(lài)spring-boot-configuration-processor后續(xù)寫(xiě)配置文件就有提示信息:
<!-- 導(dǎo)入文件處理器,加上這個(gè),以后編寫(xiě)配置就有提示了--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-configuration-processor</artifactId> <optional> true </optional> </dependency>
其他獲取配置相關(guān)內(nèi)容后續(xù)更新。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java爬蟲(chóng) 信息抓取的實(shí)現(xiàn)
本文主要介紹 Java爬蟲(chóng) 信息抓取的實(shí)現(xiàn),這里詳細(xì)介紹了如何實(shí)現(xiàn)該方法,并附示例代碼供大家學(xué)習(xí)參考,有興趣的小伙伴可以參考下2016-09-09SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例
這篇文章主要介紹了SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04maven安裝、使用、配置本地倉(cāng)庫(kù)、idea配置maven以及解決plugins報(bào)錯(cuò)問(wèn)題
本地倉(cāng)庫(kù)是遠(yuǎn)程倉(cāng)庫(kù)的一個(gè)緩沖和子集,當(dāng)你構(gòu)建Maven項(xiàng)目時(shí)首先會(huì)從本地倉(cāng)庫(kù)查找資源,如果沒(méi)有那么Maven會(huì)從遠(yuǎn)程倉(cāng)庫(kù)下載到你本地倉(cāng)庫(kù),這篇文章主要給大家介紹了關(guān)于maven安裝、使用、配置本地倉(cāng)庫(kù)、idea配置maven以及解決plugins報(bào)錯(cuò)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2024-01-01SpringBoot如何通過(guò)yml方式整合Mybatis
這篇文章主要介紹了SpringBoot如何通過(guò)yml方式整合Mybatis,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java中List使用stream流轉(zhuǎn)成map的幾種方式詳解
Stream是Java8中處理集合的關(guān)鍵抽象概念,它可以指定你希望對(duì)集合進(jìn)行的操作,可以執(zhí)行非常復(fù)雜的查找、過(guò)濾和映射數(shù)據(jù)等操作,下面這篇文章主要給大家介紹了關(guān)于Java中List使用stream流轉(zhuǎn)成map的幾種方式,需要的朋友可以參考下2023-04-04