比properties更好用的读配置文件的方式
用properties.load()时总是要求改level为6,很不舒服
学习了用别的方式读配置文件
案例如下:
一、需要的依赖有:
<!--log4j日志核心包--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><!--xml解析核心包--><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 自动生成代码插件 和本案例无关 --><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><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.Test</mainClass></transformer><transformerimplementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/spring.handlers</resource></transformer><transformerimplementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/spring.schemas</resource></transformer></transformers></configuration></execution></executions></plugin></plugins></build>
二、前提工作
resources里建 config和generator包
config里放配置文件,因为test和生产环境不一样,所以用这种方式
test.properties
cloud.mysql.jdbc.driver=com.mysql.cj.jdbc.Driver
和config同级中放config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration><!--测试环境--><properties resource="config/test.properties"></properties></configuration>
三、configuration工具
constant包中放configuration 代码如下
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 blocklog.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);//获得文档对象Element root = doc.getRootElement();//获得根节点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;}@Overridepublic 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);}
}
四、测试
写好了 最后测试一下
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);}
}
这样比直接用properties确实好用多了
比properties更好用的读配置文件的方式相关推荐
- C语言读配置文件-自动封装Windows系统服务--20210512
描述:基于Linux平台C语言编写的 读配置文件(.txt .init),交叉编译为 .exe程序:并在Windows下自动将程序注册为系统服务的模型. 环境准备: (1)Ubuntu虚拟机 (2)基 ...
- spring核心配置文件引入外部properties文件和另外的xml配置文件
spring核心配置文件引入外部properties文件和另外的xml配置文件 为什么要引入外部文件 我们使用jdbc的时候,会创建一个jdbc.properties配置文件,如果我需要在spring ...
- 计算机师范类算师范教育类吗,师范教育类专业和计算机类专业,两者相比,哪个更适合自考生报读...
原标题:师范教育类专业和计算机类专业,两者相比,哪个更适合自考生报读 一.师范教育类专业 大部分毕业生的就业方向都是在教育行业,互联网的发展,教学教育有了网络课,让人们随时随地都可以学习,也给了教育更 ...
- 贝叶斯优化: 一种更好的超参数调优方式
简介 本文受 浅析 Hinton 最近提出的 Capsule 计划 启发,希望以更通俗的方式推广机器学习算法,让有数学基础和编程能力的人能够乐享其中. 目前人工智能和深度学习越趋普及,大家可以使用开源 ...
- mysql 值为0 但却被认为null_MySQL介于普通读和锁定读的加锁方式
在阅读本文前最好先看过三篇语句加锁分析文章: 超全面MySQL语句加锁分析(上篇)(求转) 超全面MySQL语句加锁分析(中篇)(求转) 超全面MySQL语句加锁分析(下篇)(求转) 事前准备 为了故 ...
- java 配置文件加密_Spring cloud config 配置文件加密方式
前言 我们会使用git来保存我们项目的配置文件,但是文件中总有一些敏感数据,对于这些敏感数据我们通常需要给它加密,加密通常有两种加密方式,一种是对称加密,一种是非对称加密,对称加密简单方便,但是安全性 ...
- 【Python自动化任务】让运维更简单的7种定时任务实现方式,总有一种适合你的场景
想要看更加舒服的排版.更加准时的推送 关注公众号"不太灵光的程序员" 每日八点有干货推送 有粉丝留言问什么时候可以写一个关于自动化任务的文章 准备上!~ 感觉有用关注公众号 &qu ...
- GBase 8c安装部署(一):修改配置文件部署方式
GBase 8c数据库支持手工部署.修改配置文件部署和创建配置文件部署三种安装部署方式,用户可以自行选择.推荐部署方式为:修改配置文件部署方式 > 创建配置文件部署方式 > 手工部署方式. ...
- redis使用配置文件的方式启动
文章目录 为什么用配置文件启动呢 使用配置文件启动 1.创建配置文件 2.修改配置文件里面的参数 3.以配置文件的方式启动 4.查看日志文件 为什么用配置文件启动呢 因为在企业项目中不会只有单个red ...
最新文章
- 尺度空间理论与图像金字塔(二)
- 【转】学习汇编前你应该知道的知识
- SpringBoot开发案例之整合Dubbo提供者(二)
- apache2.4配置虚拟主机
- 基于Docker部署Nginx
- leetcode的回溯算法题目用这个模板解题,一网打尽,so easy!!!
- Scala基础 - _root_ package的作用
- 微软超融合私有云测试11-SCVMM2016部署之添加Hyper-V集群
- 被忽视的代码审查,往往可以事半功倍?
- DevOps使用教程 华为云(11)git分支怎么用 分支合并 评审
- C语言字符数组练习题
- java 生成war包_java利用Ant脚本生成war包全过程
- 拼多多Java面试题、笔试题(含答案)
- python2与python3实现读取网页刷访问量
- 酷比魔方iPlay40刷Magisk+Root教程
- 新版百元纸币下月发行!这些防伪你要知道
- 如何给centos设置中文、如何给火狐浏览器设置中文
- Error running SecureCardJavaApp. Command line is too long. Shorten the command line and rerun.
- @Scripts “does not exist”
- ACM/ICPC 2017 Nanning Regional 总结
热门文章
- Java Web假登录
- java datediff函数_SQL语句中DateDiff函数说明
- 大话设计模式、UML、设计模式Java版完全总结
- 基于W5300的嵌入式以太网接口设计
- 小白学python(爬虫知识3(数据保存xlwt)
- 关于旋转向量和罗德里格斯旋转公式
- linux笔记(10):ubuntu环境下,基于SDL2运行lvgl+ffmpeg播放mp4
- 平时只会用Kafka发消息,昨天突然遇到一次Kafka消息堆积生产事故!
- 很久没用我的arcmap,开机后发现打不开了
- 初中生学计算机应用好不好就业,初中毕业学习计算机应用专业怎么样?