前言:在我们的日常编程中难免会有些我们自定义的配置,虽然Java中提供了很多的读取配置文件的方法,但是当我们需要修改配置文件的key的时候,就会发现太过散乱了,工作量也会很大,涉及的文件还很多,一不小心就要出问题。那这个时候如果我们能够把所有的配置的key都放到一个文件中,其他文件需要获取配置的时候都调用这个文件,那么即使不管怎么修改配置配置文件的key,我也只需要修改这个文件,刚才的问题不就得到了完美的解决了么
废话不多说,直接上代码
项目目录结构
maven依赖:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 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
自定义配置文件:
this.custom.config.content = 这是我的自定义配置内容
配置文件加载类:
package com.sxy.propertiesreaddemo.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; import java.util.Properties; /** * @Description 文件载入工具类. * 可载入多个properties文件, * 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先. * @Author SuXingYong * @Date 2020/7/11 20:55 **/ @Slf4j public class PropertiesLoader { private static ResourceLoader resourceLoader = new DefaultResourceLoader(); private final Properties properties; public PropertiesLoader(String... resourcesPaths) { properties = loadProperties(resourcesPaths); } /** * @Description 取出Property,但以System的Property优先,取不到返回空字符串. * @Author SuXingYong * @Date 2020/7/11 20:55 * @Param [key] * @param key 配置文件的key * @Return java.lang.String **/ private String getValue(String key) { String systemProperty = System.getProperty(key); if (systemProperty != null) { return systemProperty; } if (properties.containsKey(key)) { return properties.getProperty(key); } return ""; } /** * @Description 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常. * @Author SuXingYong * @Date 2020/7/11 20:55 * @Param [key] * @Return java.lang.String **/ public String getProperty(String key) { String value = getValue(key); if (value == null) { throw new NoSuchElementException(); } return value; } /** * @Description 载入多个文件, 文件路径使用Spring Resource格式. * @Author SuXingYong * @Date 2020/7/11 20:55 * @Param [resourcesPaths] * @Return java.util.Properties **/ private Properties loadProperties(String... resourcesPaths) { Properties props = new Properties(); 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); } } return props; } }
配置文件工具类:
package com.sxy.propertiesreaddemo.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; /** * @Description 读取配置文件 * @Author SuXingYong * @Date 2020/7/11 20:55 **/ @Slf4j public class PropertiesUtils { /** * @Description 获取配置文件加载 * @Author SuXingYong * @Date 2020/7/11 20:55 * @Param [configPath] * @param configPath 配置文件路径 * @Return com.sxy.platform.properties.PropertiesLoader **/ public static PropertiesLoader getProperties(String configPath) { PropertiesLoader property=new PropertiesLoader(configPath); return property; } /** * @Description 根据key获取value * @Author SuXingYong * @Date 2020/7/11 20:55 * @Param [key, properties] * @Return java.lang.String **/ public static String getPropertyValue(String key, PropertiesLoader properties) { String value = properties.getProperty(key); return value; } }
配置文件统一对外调用类:
package com.sxy.propertiesreaddemo.utils; /** * @Description config配置文件读取类 * @By SuXingYong * @DateTime 2020/7/11 20:55 **/ public class PropertyConfigValue { /** * 配置加载器 */ private static PropertiesLoader 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 static String getThisCustomConfigContent(){ String value = PropertiesUtils.getPropertyValue(THIS_CUSTOM_CONFIG_CONTENT, propertiesLoader); return value; } }
springboot项目启动类:
package com.sxy.propertiesreaddemo; import com.sxy.propertiesreaddemo.utils.PropertyConfigValue; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @Slf4j @SpringBootApplication public class PropertiesReadDemoApplication { public static void main(String[] args) { SpringApplication.run(PropertiesReadDemoApplication.class, args); log.info("this.custom.config.content = "+ PropertyConfigValue.getThisCustomConfigContent()); } }
运行结果: