spring boot读取自定义配置文件,springboot读取配置的方式

  spring boot读取自定义配置文件,springboot读取配置的方式

  

目录

一、读取系统配置文件应用程序.格式二、读取自定义配置文件性能格式内容三、读取自定义配置文件格式格式内容四、其他扩展内容

 

  

一、读取系统配置文件application.yaml

1、应用程序配置文件中增加一下测试配置

 

  测试数据:动物:姓氏:动物年龄18 boss:真实出生2022/02/22映射: { key 1: value 1,key 2: value 2 }列表:[狗、猫、房子狗:名字:旺财年龄: 32,新建实体实体类动物

  进口龙目岛。数据;导入org。spring框架。靴子。语境。属性。配置属性;导入org。spring框架。刻板印象。组件;导入Java。util。日期;导入Java。util。列表;导入Java。util。地图;@Component//标识为bean @ configuration属性(前缀=测试数据。动物)//前缀前缀需要和阳明海运股份有限公司配置文件里的匹配.Data @ Data//这个是一个龙目岛注解,用于生成gettersetter方法公共类动物{私有字符串姓私人年龄;私布尔老板私人出生日期;私有地图字符串,字符串映射;私人列表字符串列表;私狗狗;}3、新建实体实体类狗

  包com。举例。演示。db。配置;进口龙目岛。数据;导入org。spring框架。刻板印象。组件;@Component//标识为Bean@Data//这个是一个龙目岛注解,用于生成gettersetter方法@Configuration//标识是一个配置类公共类狗{私有字符串名称;私人年龄;}4、新建测试类测试数据库库名

  导入com。举例。演示。db。配置。远程属性;导入朱尼特。框架。测试用例;导入org。朱尼特。测试;导入org。朱尼特。奔跑者。与.一起跑;导入org。spring框架。豆子。工厂。注释。自动连线;导入组织。spring框架。靴子。语境。属性。enableconfigurationproperties导入org。spring框架。靴子。测试。语境。弹簧靴测试;导入org。spring框架。测试。语境。JUnit 4。春天JUnit 4级跑者;@ run with(spring JUnit 4 class runner。class)//让测试运行于弹簧测试环境@ spring boot test(classes=演示应用程序。class)//指定启动类@ EnableConfigurationProperties(远程属性。class)//使远程属性注解类生效公共类我的测试{ @ Autowired Animal Animal@ Test public void Test 2(){ system。出去。println( person==== animal);系统。出去。println(年龄=======动物。getage());系统。出去。println(狗。姓名======动物。得到狗().getName()

  dog.age===="+animal.getDog().getAge()); }}5、运行结果:

  

 

  

 

  

二、读取自定义配置文件properties格式内容

1、resourcesconfig目录下新建remote.properties配置文件,内容如下:

 

  

remote.testname=张三remote.testpass=123456remote.testvalue=ceshishuju

2、新建entity实体类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//标识为Beanpublic 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)//让测试运行于Spring测试环境@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("-----------:"+testpass); System.out.println("------------:"+remoteProperties.getTestvalue()); }}

4、运行结果:

 

  

 

  

 

  

三、读取自定义配置文件yaml格式内容

1、resourcesconfig目录下新建remote.yaml配置文件,内容如下:

 

  

remote: person: testname: 张三 testpass: 123456 testvalue: kkvalue

2、新建工厂转换类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.xxpublic 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实体类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)//配置文件路径,配置转换类@Data//这个是一个lombok注解,用于生成getter&setter方法@Component//标识为Beanpublic class RemoteProperties { @Value("${remote.person.testname}")//根据配置文件写全路径 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)//让测试运行于Spring测试环境@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、运行结果:

 

  

 

  说明:

  

 

  这里需要写一个工厂去读取propertySource(在调试的时候我看到默认读取的方式是xx.xx.xx而自定义的yml配置文件是每一个xx都是分开的,所以不能获取到,而自己创建的配置类MyPropertySourceFactory就是需要把自定义配置文件.yml的读取方式变成跟application的读取方式一致的 xx.xx.xx,并且通过@Value注解指定变量的的关系和yaml配置文件对应)

  

 

  

四、其他扩展内容

可以加入依赖spring-boot-configuration-processor后续写配置文件就有提示信息:

 

  

<!-- 导入文件处理器,加上这个,以后编写配置就有提示了--><dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-configuration-processor</artifactId> <optional> true </optional></dependency>

其他获取配置相关内容后续更新。

 

  以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: