最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot讀取自定義配置文件方式(properties,yaml)

 更新時(shí)間:2022年07月08日 10:48:00   作者:zhanhjxxx  
這篇文章主要介紹了SpringBoot讀取自定義配置文件方式(properties,yaml),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、讀取系統(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í)體類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í)體類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è)配置類
public class Dog {
    private String name;
    private int age;
}

4、新建測(cè)試類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)類
@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//表明這是一個(gè)配置類
@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è)試類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)類
@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//表明這是一個(gè)配置類
@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)換類
@Data//這個(gè)是一個(gè)lombok注解,用于生成getter&setter方法
@Component//標(biāo)識(shí)為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、新建測(cè)試類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)類
@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é)果:

 說(shuō)明:

 這里需要寫一個(gè)工廠去讀取propertySource(在調(diào)試的時(shí)候我看到默認(rèn)讀取的方式是xx.xx.xx而自定義的yml配置文件是每一個(gè)xx都是分開(kāi)的,所以不能獲取到,而自己創(chuàng)建的配置類MyPropertySourceFactory就是需要把自定義配置文件.yml的讀取方式變成跟application的讀取方式一致的 xx.xx.xx,并且通過(guò)@Value注解指定變量的的關(guān)系和yaml配置文件對(duì)應(yīng))

四、其他擴(kuò)展內(nèi)容

可以加入依賴spring-boot-configuration-processor后續(xù)寫配置文件就有提示信息:

<!-- 導(dǎo)入文件處理器,加上這個(gè),以后編寫配置就有提示了-->
<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 語(yǔ)言守護(hù)線程 Daemon Thread使用示例詳解

    Java 語(yǔ)言守護(hù)線程 Daemon Thread使用示例詳解

    這篇文章主要為大家介紹了Java 語(yǔ)言守護(hù)線程 Daemon Thread使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java遞歸 遍歷目錄的小例子

    Java遞歸 遍歷目錄的小例子

    Java遞歸 遍歷目錄的小例子,需要的朋友可以參考一下
    2013-03-03
  • Spring Boot集成Kafka的示例代碼

    Spring Boot集成Kafka的示例代碼

    本篇文章主要介紹了Spring Boot集成Kafka的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • SpringBoot請(qǐng)求參數(shù)相關(guān)注解說(shuō)明小結(jié)

    SpringBoot請(qǐng)求參數(shù)相關(guān)注解說(shuō)明小結(jié)

    這篇文章主要介紹了SpringBoot請(qǐng)求參數(shù)相關(guān)注解說(shuō)明,主要包括@PathVariable,@RequestHeader、@CookieValue、@RequestBody和@RequestParam,本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • SpringBoot解決ajax跨域問(wèn)題的方法

    SpringBoot解決ajax跨域問(wèn)題的方法

    這篇文章主要為大家詳細(xì)介紹了SpringBoot解決ajax跨域問(wèn)題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java如何在 Word 中設(shè)置上、下標(biāo)

    Java如何在 Word 中設(shè)置上、下標(biāo)

    這篇文章主要介紹了Java如何在 Word 中設(shè)置上、下標(biāo),幫助大家更好的利用Java處理文檔,感興趣的朋友可以了解下
    2020-09-09
  • 解析springboot集成AOP實(shí)現(xiàn)日志輸出的方法

    解析springboot集成AOP實(shí)現(xiàn)日志輸出的方法

    如果這需要在每一個(gè)controller層去寫的話代碼過(guò)于重復(fù),于是就使用AOP定義切面 對(duì)其接口調(diào)用前后進(jìn)行攔截日志輸出。接下來(lái)通過(guò)本文給大家介紹springboot集成AOP實(shí)現(xiàn)日志輸出,需要的朋友可以參考下
    2021-11-11
  • springboot集成Mybatis-plus-join-boot-start詳解

    springboot集成Mybatis-plus-join-boot-start詳解

    這篇文章主要介紹了springboot集成Mybatis-plus-join-boot-start方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java經(jīng)典算法匯總之選擇排序(SelectionSort)

    Java經(jīng)典算法匯總之選擇排序(SelectionSort)

    選擇排序也是比較簡(jiǎn)單的一種排序方法,原理也比較容易理解,選擇排序在每次遍歷過(guò)程中只記錄下來(lái)最小的一個(gè)元素的下標(biāo),待全部比較結(jié)束之后,將最小的元素與未排序的那部分序列的最前面一個(gè)元素交換,這樣就降低了交換的次數(shù),提高了排序效率。
    2016-04-04
  • Java如何將時(shí)間戳格式化為日期字符串

    Java如何將時(shí)間戳格式化為日期字符串

    這篇文章主要介紹了Java如何將時(shí)間戳格式化為日期字符串問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04

最新評(píng)論

蓬溪县| 轮台县| 两当县| 翼城县| 德化县| 赤水市| 昌吉市| 福海县| 鸡泽县| 右玉县| 开鲁县| 凤城市| 静乐县| 蛟河市| 台南市| 无为县| 吉安县| 固镇县| 泗洪县| 宁国市| 仪征市| 海淀区| 永寿县| 宾阳县| 濮阳县| 绥滨县| 黄浦区| 丹棱县| 六安市| 冕宁县| 济南市| 西乌珠穆沁旗| 叙永县| 邹城市| 龙江县| 广安市| 呼伦贝尔市| 长垣县| 三江| 五指山市| 溧阳市|