现在很多项目都是ssh框架,其中applicationContext.xml必定会用引用外部properties文件,
怎样即可供自己调用,又可提供给外部调用呢,下介绍个方法:
直接上代码吧!哈哈
1. jdbc.properties 内容如下:
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
2. 自己调用 applicationContext.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
<!-- 动态读取配置文件进行数据加载 -->
<bean id="propertyConfigurer"
class="com.sse.common.util.CustomizedPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 这种是用于工程当中读取<value>classpath*:jdbc.properties</value> -->
<!-- 下边这种文法用于jar包,访问jar包外部的文件配置方式 -->
<value>file:./jdbc.properties</value>
</list>
</property>
</bean>
<!-- 动态读取配置文件进行数据加载
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties" />
</bean> -->
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="${jdbc.driverClass}" />
</bean>
</beans>
3. 处部调用实现类CustomizedPropertyPlaceholderConfigurer.java 继承PropertyPlaceholderConfigurer.java 内容如下:
package com.sse.common.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 自定义PropertyPlaceholderConfigurer返回properties内容
* Created on 2013-12-12
* <p>Title: XXXX系统_[模块名]/p>
* <p>Description: [描述该类概要功能介绍]</p>
* <p>Copyright: Copyright (c) 2011</p>
* <p>Company: </p>
* <p>Department: 网站运维部</p>
* @author zhl
*/
public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static Map<String, Object> ctxPropertiesMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}
4. 外部类调用 获取属性文件的内容
String host = (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("jdbc.driverClass");