加载配置文件(xml文件,properties文件)demo
关于加载Properties配置文件。
public class ContextPropertiesUtil {
private static ContextPropertiesUtil INSTANCE;
private static Properties properties;
public static final String PROPERTIES_FILE_NAME = "context-cfsss.properties";
private ContextPropertiesUtil(){
properties = new Properties();
InputStream in = ContextPropertiesUtil.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME);
try {
properties.load(in);
in.close();
} catch (IOException e) {
LogUtil.debug("加载文件失败");
}
}
public static ContextPropertiesUtil getInstance(){
if (INSTANCE == null){
INSTANCE = new ContextPropertiesUtil();
}
return INSTANCE;
}
public String getProperty(String key){
return properties.getProperty(key);
}
public static String getProperties(String key){
return ContextPropertiesUtil.getInstance().getProperty(key);
}
public static void main(String[] args) {
System.out.println(ContextPropertiesUtil.getProperties("cfsss.txnActionBean.jndi.url"));
}
}
关于加载xml文件。并需要集成类
demo
<mqcpConfig>
<consumers>
<consumer>
<consumerId>abbbb${cccc}</consumerId>
<topics>
<topic>
<topicId>aaa</topicId>
<tokenId>bbb</tokenId>
<serviceBeanName>commonWithDrawalCounter</serviceBeanName>
</topic>
<topic>
<topicId>ccc</topicId>
<tokenId>ddd</tokenId>
<serviceBeanName>commonNoticePersistence</serviceBeanName>
</topic>
</topics>
</consumer>
</consumers>
</mqcpConfig>
读取xml文件。并封装成List。并通过反射的方法调用StringBean.
public class ParseXmlDocument {
private static final Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
public static void main(String[] args) {
// read xml
InputStream stream = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); // 安全防护,防止xml实体攻击
DocumentBuilder db = dbf.newDocumentBuilder();
// 加载xml文件
stream = MqcpCommonConsumer.class.getClassLoader().getResourceAsStream("mqcp-customer.xml");
Document document = db.parse(stream);
// 获取标签
NodeList consumerTableList = document.getElementsByTagName("consumer");
Map<String, String> isRepeatMap = new HashMap<String, String>();
for (int i = 0; i < consumerTableList.getLength(); i++) {
// 获取标签下的子标签
NodeList tableNode = consumerTableList.item(i).getChildNodes();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int j = 0; j < tableNode.getLength(); j++) {
// 得到子标签对应的值
String contenxtValue = tableNode.item(j).getTextContent();
// 替换值中的通配符里的值。
contenxtValue = getPropertiesValue(contenxtValue);
// 子标签的标签名
String contenxtName = tableNode.item(j).getNodeName();
if ("CONSUMERID".equalsIgnoreCase(contenxtName)) {
if (StringUtils.isNotBlank(isRepeatMap.get(contenxtValue))) {
throw new Exception("同一个consumerId不能重复启动,请确保在mqcp-customer.xml以及其他配置中不重复");
} else {
isRepeatMap.put(contenxtValue, "OK");
}
// 初始化值
String mpcqConsumer = contenxtValue;
System.out.println(mpcqConsumer);
}
if ("TOPICS".equalsIgnoreCase(contenxtName)) {
NodeList topicsTableNode = tableNode.item(j).getChildNodes();
for (int k = 0; k < topicsTableNode.getLength(); k++) {
String topics = "";
String serviceBeanName = "";
Map<String, Object> map = new HashMap<String, Object>();
if ("TOPIC".equalsIgnoreCase(topicsTableNode.item(k).getNodeName())) {
NodeList topicTableNode = topicsTableNode.item(k).getChildNodes();
for (int x = 0; x < topicTableNode.getLength(); x++) {
String topicContext = topicTableNode.item(x).getTextContent();
topicContext = getPropertiesValue(topicContext);
String topicName = topicTableNode.item(x).getNodeName();
if ("TOKENID".equalsIgnoreCase(topicName)) {
map.put("tokenId", topicContext);
}
if ("TOPICID".equalsIgnoreCase(topicName)) {
topics = topicContext;
map.put("topicId", topics);
}
if ("SERVICEBEANNAME".equalsIgnoreCase(topicName)) {
serviceBeanName = topicContext;
map.put("serviceBeanName", serviceBeanName);
}
}
}
System.out.println(map);
if (!map.isEmpty()) {
list.add(map);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != stream) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 格式化字符串 字符串中使用${key}表示占位符
*
* @param sourStr
* 需要匹配的字符串
* @param param
* 参数集
* @return
*/
private static String getPropertiesValue(String sourStr) {
if (sourStr == null) {
return null;
}
Matcher matcher = pattern.matcher(sourStr);
while (matcher.find()) {
String key = matcher.group();
String keyclone = key.substring(2, key.length() - 1).trim();
String value = MqcpPropertiesUtil.getInstance().getProperty(keyclone);
if (value == null) {
// 獲取通配符中key對應的值
value = ContextPropertiesUtil.getProperties(keyclone);
}
if (value != null) {
sourStr = sourStr.replace(key, value);
}
}
return sourStr;
}
根据serviceBeanName注入Sping容器。
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
}
}
加载配置文件(xml文件,properties文件)demo相关推荐
- idea使用c3p0数据库连接池无法加载配置文件xml,配置文件放置的位置
注意:要把c3p0-config.xml文件放在输出的文件夹里面,即放在out里面,而不是放在src里面.(如下图) 至于配置文件怎么写,可以参考其他经验教程,这里不再赘述.
- spring核心配置文件引入外部properties文件和另外的xml配置文件
spring核心配置文件引入外部properties文件和另外的xml配置文件 为什么要引入外部文件 我们使用jdbc的时候,会创建一个jdbc.properties配置文件,如果我需要在spring ...
- spring-自动加载配置文件\使用属性文件注入
在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到W ...
- springboot mybatis 热加载mapper.xml文件(最简单)
大家好,我是烤鸭: 今天介绍一下springboot mybatis 热加载mapper.xml文件. 本来不打算写的,看到网上比较流行的方式都比较麻烦,想着简化一下. 网上流行的版本. https: ...
- SpringBoot+Mybatis加载Mapper.xml文件的两种方式
前言:我们在平常工作中用到mybatis去加载Mapper.xml文件,可能mapper文件放的路径不一样,由此我们需要配置多个路径,幸运的是Mybatis支持我们配置多个不同路径.现在介绍两种方法. ...
- SpringBoot 加载不出来application.yml文件
摘要 记录一次SpringBoot加载不出来application.yml文件的问题解决过程 问题 配置了application.yml文件,但是映射到properties bean的时候失败 @Co ...
- java通过spring获取配置文件_springboot获取properties文件的配置内容(转载)
1.使用@Value注解读取 读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Nam ...
- Pandas将dataframe保存为pickle文件并加载保存后的pickle文件查看dataframe数据实战
Pandas将dataframe保存为pickle文件并加载保存后的pickle文件查看dataframe数据实战 目录 Pandas将dataframe保存为pickle文件并加载保存后的pickl ...
- mysql中鼠标光标消失了_为什么我这里没有显示鼠标的悬停可改变页面颜色,以为什么我加载了mysql的jar文件还是不能显示报表的内容呢?...
源自:3-6 JSP页面实现 为什么我这里没有显示鼠标的悬停可改变页面颜色,以为什么我加载了mysql的jar文件还是不能显示报表的内容呢? 首先是index.jsp pageEncoding=&qu ...
- python训练好的图片验证_利用keras加载训练好的.H5文件,并实现预测图片
我就废话不多说了,直接上代码吧! import matplotlib matplotlib.use('Agg') import os from keras.models import load_mod ...
最新文章
- 一文详解,jvm内存分代与垃圾回收原理
- python的速度问题_python编程如何提升速度篇
- delphi XE 下打开内存泄漏调试功能
- 游戏动作师使用Unity3D遇到过的所有问题
- 你有一个向LiveVideoStackCon讲师提问的机会
- [导入]不需要任何附加信息的伪凹凸光照计算方法。
- php网页示例,新手入门:初学动态网页PHP的18个例子
- 安装adt-bundle-windows-x86-20130917时遇到的问题及解决方法
- 给你的站点添加 DNS CAA 保护
- ai带来的革命_AI革命就在这里。 这与我们预期的不同。
- RC串联延时电路电容充电时间计算
- win10计算机安全策略设置,win10系统重置本地安全策略所有设置的操作方法
- 对多频外差的改进-校正伽马误差
- python 91图片站爬虫
- 【阅读文献】单目视觉SLAM方法综述【4】~特征点深度获取+地图尺度控制
- IDEA 安装与破解(亲测有效)
- 基于ZigBee和STM32的智能家居控制系统的设计与实现(四)
- PathInfo模式的支持
- 四步轻松实现用Visio画UML类图
- oracle游标列转行,Oracle行转列和列转行
热门文章
- 关于瑞昱8763bfr的学习总结(1)
- SM2258XT提示flash mixed different grade错误怎么破,附解决办法
- 慧荣SM2258XT、SM2259XT量产工具开启“忽略区分等级”功能
- 华为MateBook14s更换固态(系统无缝衔接,等价于官方镜像)
- 在renderman中使用raytrace 计算fur的项目
- java 网站计数器_网站计数器——Java实现
- avr 运行 linux,linux(ubuntu9.04)安装avr编译环境
- BP神经网络介绍及算法实现
- 小学生python编程教程-python 小学生教程|怎么让一个小学生学会Python?
- php写猴子搬香蕉问题,世界500强企业面试题:猴子吃香蕉