1. 程式人生 > 實用技巧 >java的自定義配置檔案統一讀取配置類示例

java的自定義配置檔案統一讀取配置類示例

前言:在我們的日常程式設計中難免會有些我們自定義的配置,雖然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>

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

配置檔案工具類:

PropertiesUtils.java

配置檔案統一對外呼叫類:

PropertyConfigValue.java

springboot專案啟動類:

PropertiesReadDemoApplication.java

執行結果: