当前位置:网站首页>Better way to read configuration files than properties
Better way to read configuration files than properties
2022-04-23 04:42:00 【Z-hhhhh】
use properties.load() Always ask to change level by 6, Very uncomfortable
Learned to read configuration files in other ways
The case is as follows :
One 、 The dependencies needed are :
<!--log4j Log core package -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!--xml Parse core package -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.5</version>
</dependency>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.log</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!-- mybatis generator Auto generate code plug-in Nothing to do with this case -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.Test</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Two 、 If work
resources Li Jian config and generator package
config Put the configuration file in , because test Different from the production environment , So in this way
test.properties
cloud.mysql.jdbc.driver=com.mysql.cj.jdbc.Driver
and config In the same level config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!-- Test environment -->
<properties resource="config/test.properties"></properties>
</configuration>
3、 ... and 、configuration Tools
constant Bao Zhongfang configuration The code is as follows
package constant;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
/** * @author zwh * @date 2022/4/20 */
public class Configuration extends Properties {
public static final Logger log = LogManager.getLogger(Configuration.class);
private static final long serialVersionUID = 50440463580273222L;
private static Configuration instance = null;
public Configuration() {
InputStream in;
try {
in = this.getClass().getResourceAsStream("/"+ read());
this.load(in);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error(e);
}
}
public static synchronized Configuration getInstance() {
if (instance == null) {
instance = new Configuration();
}
return instance;
}
public String read() {
String prefix = null;
SAXBuilder builder = new SAXBuilder();
InputStream file = this.getClass().getResourceAsStream("/config.xml");
try {
Document doc = builder.build(file);// Get document object
Element root = doc.getRootElement();// Get root node
List<Element> list = root.getChildren();
for (Element e : list) {
if (null != e.getAttribute("resource")) {
prefix = e.getAttribute("resource").getValue();
}
}
} catch (Exception e) {
log.error(e);
}
return prefix;
}
@Override
public String getProperty(String key, String defaultValue) {
String val = getProperty(key);
return (val == null || val.isEmpty()) ? defaultValue : val;
}
public String getString(String name, String defaultValue) {
return this.getProperty(name, defaultValue);
}
public int getInt(String name, int defaultValue) {
String val = this.getProperty(name);
return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);
}
public long getLong(String name, long defaultValue) {
String val = this.getProperty(name);
return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);
}
public float getFloat(String name, float defaultValue) {
String val = this.getProperty(name);
return (val == null || val.isEmpty()) ? defaultValue : Float.parseFloat(val);
}
public double getDouble(String name, double defaultValue) {
String val = this.getProperty(name);
return (val == null || val.isEmpty()) ? defaultValue : Double.parseDouble(val);
}
public byte getByte(String name, byte defaultValue) {
String val = this.getProperty(name);
return (val == null || val.isEmpty()) ? defaultValue : Byte.parseByte(val);
}
}
Four 、 test
Well done. Finally, test it
package com;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/** * @author zwh * @date 2022/4/20 */
public class Test {
public static final Logger log = LogManager.getLogger(Test.class);
public static constant.Configuration config = constant.Configuration.getInstance();
public static void main(String[] args) {
String driverClassName = config.getString("cloud.mysql.jdbc.driver", "");
System.out.println(driverClassName);
}
}
This is better than using properties It's really much easier to use
版权声明
本文为[Z-hhhhh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220559122046.html
边栏推荐
- 2019 is coming to an end, the longest day.
- Key points of AWS eks deployment and differences between console and eksctl creation
- 補:注解(Annotation)
- Leetcode004 -- Roman numeral to integer
- SQL statement for adding columns in MySQL table
- Improving 3D object detection with channel wise transformer
- Recommended scheme for national production of electronic components of wireless keyboard
- 【论文阅读】【3d目标检测】point transformer
- 520. Detect capital letters
- Supplement 14: cmake practice project notes (to be continued 4 / 22)
猜你喜欢

383. 赎金信

优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验

Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000

Supplement 14: cmake practice project notes (to be continued 4 / 22)

Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.

AWS EKS添加集群用户或IAM角色

Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU

MYSQL查询至少连续n天登录的用户

Installation and use of Apache bench (AB pressure test tool)

AWS eks add cluster user or Iam role
随机推荐
针对NFT的网络钓鱼
三十六计是什么
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
Spark FAQ sorting - must see before interview
Supplement 14: cmake practice project notes (to be continued 4 / 22)
zynq平臺交叉編譯器的安裝
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
Redis 命令大全
unity摄像机旋转带有滑动效果(自转)
从MySQL数据库迁移到AWS DynamoDB
Mysql, binlog log query
Logger and zap log Library in go language
Leetcode006 -- find the longest common prefix in the string array
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Open the past and let's start over.
win10, mysql-8.0.26-winx64. Zip installation
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
【Pytorch基础】torch.split()用法
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.