关于加载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);
}
}