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


<?xml version="1.0" encoding="UTF-8"?> <project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //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>
pom.xml
自定義配置文件:


this.custom.config.content = 這是我的自定義配置內容
custom-config.properties
配置文件載入類:


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; } }
PropertiesLoader.java
配置文件工具類:

配置文件統一對外調用類:

springboot項目啟動類:

運行結果: