当前位置:网站首页>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
边栏推荐
- 383. Ransom letter
- Unity3D 实用技巧 - 理论知识库(一)
- 做数据可视化应该避免的8个误区
- How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
- 520. Detect capital letters
- AWS EKS添加集群用户或IAM角色
- The perfect combination of collaborative process and multi process
- Innovative practice of short video content understanding and generation technology in meituan
- win10, mysql-8.0.26-winx64. Zip installation
- 【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
猜你喜欢
Eight misunderstandings that should be avoided in data visualization
C language: spoof games
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
Recommended scheme of national manufactured electronic components for intelligent electronic scales
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Small volume Schottky diode compatible with nsr20f30nxt5g
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
[paper reading] [3D target detection] point transformer
PIP3 installation requests Library - the most complete pit sorting
随机推荐
【论文阅读】【3d目标检测】point transformer
IEEE Transactions on industrial information (TII)
MYSQL查询至少连续n天登录的用户
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
test
A new method for evaluating the quality of metagenome assembly - magista
Coinbase:关于跨链桥的基础知识、事实和统计数据
Redis command Encyclopedia
shell wc (统计字符数量)的基本使用
Record the blind injection script
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Chapter 4 - understanding standard equipment documents, filters and pipelines
Spark FAQ sorting - must see before interview
Jetpack 之 LifeCycle 组件使用详解
Eksctl deploying AWS eks
Leetcode001 -- returns the subscript of the array element whose sum is target
A lifetime of needs, team collaboration can play this way on cloud nailing applet
Recommended scheme for national production of electronic components of wireless keyboard
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)