java自定义配置文件_java的自定义配置文件统一读取配置类示例

前言:在我们的日常编程中难免会有些我们自定义的配置,虽然Java中提供了很多的读取配置文件的方法,但是当我们需要修改配置文件的key的时候,就会发现太过散乱了,工作量也会很大,涉及的文件还很多,一不小心就要出问题。那这个时候如果我们能够把所有的配置的key都放到一个文件中,其他文件需要获取配置的时候都调用这个文件,那么即使不管怎么修改配置配置文件的key,我也只需要修改这个文件,刚才的问题不就得到了完美的解决了么

废话不多说,直接上代码

项目目录结构

de5ae880a3b58b31dee41e3fd8044ce7.png

maven依赖:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.5.RELEASE

com.sxy

properties-read-demo

0.0.1-SNAPSHOT

properties-read-demo

Demo project for Spring Boot

1.8

2.5

org.springframework.boot

spring-boot-starter

org.projectlombok

lombok

true

commons-io

commons-io

${commons.io.version}

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

pom.xml

自定义配置文件:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

this.custom.config.content = 这是我的自定义配置内容

custom-config.properties

配置文件加载类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxy.propertiesreaddemo.utils;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.io.IOUtils;importorg.springframework.core.io.DefaultResourceLoader;importorg.springframework.core.io.Resource;importorg.springframework.core.io.ResourceLoader;importjava.io.IOException;importjava.io.InputStream;importjava.util.NoSuchElementException;importjava.util.Properties;/*** @Description 文件载入工具类.

* 可载入多个properties文件,

* 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.

* @Author SuXingYong

* @Date 2020/7/11 20:55

**/@Slf4jpublic classPropertiesLoader {private static ResourceLoader resourceLoader = newDefaultResourceLoader();private finalProperties properties;publicPropertiesLoader(String... resourcesPaths) {

properties=loadProperties(resourcesPaths);

}/*** @Description 取出Property,但以System的Property优先,取不到返回空字符串.

* @Author SuXingYong

* @Date 2020/7/11 20:55

* @Param [key]

*@paramkey 配置文件的key

* @Return java.lang.String

**/

privateString getValue(String key) {

String systemProperty=System.getProperty(key);if (systemProperty != null) {returnsystemProperty;

}if(properties.containsKey(key)) {returnproperties.getProperty(key);

}return "";

}/*** @Description 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.

* @Author SuXingYong

* @Date 2020/7/11 20:55

* @Param [key]

* @Return java.lang.String

**/

publicString getProperty(String key) {

String value=getValue(key);if (value == null) {throw newNoSuchElementException();

}returnvalue;

}/*** @Description 载入多个文件, 文件路径使用Spring Resource格式.

* @Author SuXingYong

* @Date 2020/7/11 20:55

* @Param [resourcesPaths]

* @Return java.util.Properties

**/

privateProperties loadProperties(String... resourcesPaths) {

Properties props= newProperties();for(String location : resourcesPaths) {

log.debug("Loading properties file from:" +location);

InputStream is= null;try{

Resource resource=resourceLoader.getResource(location);

is=resource.getInputStream();

props.load(is);

}catch(IOException ex) {

log.info("Could not load properties from path:" + location + ", " +ex.getMessage());

}finally{

IOUtils.closeQuietly(is);

}

}returnprops;

}

}

PropertiesLoader.java

配置文件工具类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxy.propertiesreaddemo.utils;importlombok.extern.slf4j.Slf4j;importorg.springframework.core.io.DefaultResourceLoader;importorg.springframework.core.io.ResourceLoader;/*** @Description 读取配置文件

* @Author SuXingYong

* @Date 2020/7/11 20:55

**/@Slf4jpublic classPropertiesUtils {/*** @Description 获取配置文件加载

* @Author SuXingYong

* @Date 2020/7/11 20:55

* @Param [configPath]

*@paramconfigPath 配置文件路径

* @Return com.sxy.platform.properties.PropertiesLoader

**/

public staticPropertiesLoader getProperties(String configPath) {

PropertiesLoader property=newPropertiesLoader(configPath);returnproperty;

}/*** @Description 根据key获取value

* @Author SuXingYong

* @Date 2020/7/11 20:55

* @Param [key, properties]

* @Return java.lang.String

**/

public staticString getPropertyValue(String key, PropertiesLoader properties) {

String value=properties.getProperty(key);returnvalue;

}

}

PropertiesUtils.java

配置文件统一对外调用类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxy.propertiesreaddemo.utils;/*** @Description config配置文件读取类

* @By SuXingYong

* @DateTime 2020/7/11 20:55

**/

public classPropertyConfigValue {/*** 配置加载器*/

private staticPropertiesLoader propertiesLoader;/*** @Description 自定义配置内容

* @Author SuXingYong

* @Date 2020/7/11 20:55

**/

private static String THIS_CUSTOM_CONFIG_CONTENT = "this.custom.config.content";static{

propertiesLoader= PropertiesUtils.getProperties("classpath:config/custom-config.properties");

}/*** @Description 获取 自定义配置内容

* @Author SuXingYong

* @Date 2020/6/24 15:10

* @Param []

* @Return java.lang.String

**/

public staticString getThisCustomConfigContent(){

String value=PropertiesUtils.getPropertyValue(THIS_CUSTOM_CONFIG_CONTENT, propertiesLoader);returnvalue;

}

}

PropertyConfigValue.java

springboot项目启动类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.sxy.propertiesreaddemo;importcom.sxy.propertiesreaddemo.utils.PropertyConfigValue;importlombok.extern.slf4j.Slf4j;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j

@SpringBootApplicationpublic classPropertiesReadDemoApplication {public static voidmain(String[] args) {

SpringApplication.run(PropertiesReadDemoApplication.class, args);

log.info("this.custom.config.content = "+PropertyConfigValue.getThisCustomConfigContent());

}

}

PropertiesReadDemoApplication.java

运行结果:

5db6fbf7f6293684d7d3c597e113c049.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值