前言:在我们的日常编程中难免会有些我们自定义的配置,虽然Java中提供了很多的读取配置文件的方法,但是当我们需要修改配置文件的key的时候,就会发现太过散乱了,工作量也会很大,涉及的文件还很多,一不小心就要出问题。那这个时候如果我们能够把所有的配置的key都放到一个文件中,其他文件需要获取配置的时候都调用这个文件,那么即使不管怎么修改配置配置文件的key,我也只需要修改这个文件,刚才的问题不就得到了完美的解决了么
废话不多说,直接上代码
项目目录结构
maven依赖:
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
自定义配置文件:
this.custom.config.content = 这是我的自定义配置内容
custom-config.properties
配置文件加载类:
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
配置文件工具类:
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
配置文件统一对外调用类:
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项目启动类:
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
运行结果: