前言:在我們的日常編程中難免會有些我們自定義的配置,雖然Java中提供了很多的讀取配置文件的方法,但是當我們需要修改配置文件的key的時候,就會發現太過散亂了,工作量也會很大,涉及的文件還很多,一不小心就要出問題。那這個時候如果我們能夠把所有的配置的key都放到一個文件中,其他文件需要獲取配置的時候都調用這個文件,那么即使不管怎么修改配置配置文件的key,我也只需要修改這個文件,剛才的問題不就得到了完美的解決了么
廢話不多說,直接上代碼
項目目錄結構
maven依賴:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sxy</groupId> <artifactId>properties-read-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>properties-read-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <commons.io.version>2.5</commons.io.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons.io.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
自定義配置文件:

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()); } }
運行結果: