Spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <description>应用组件</description> <!-- Configurer that replaces ${...} placeholders with values from a properties file --> <bean id="propertyConfigurer" class="com.jiewen.commons.toolkit.config.XmlExtPropertyPlaceholderConfigurer"> <property name="extLocation" value="classpath:config.xml"/> </bean> <bean id="loadKeyFromProperties" class="com.test.init.LoadKeyFormProperties"> <property name="keyFileResource"> <value>classpath:keys.properties</value> </property> </bean>
<!-- DataSource -->
<bean id="dataSource" class="com.jiewen.commons.toolkit.db.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${config.jdbc.driver}</value></property>
<property name="url"><value>${config.jdbc.url}</value></property>
<property name="username"><value>${config.jdbc.username}</value></property>
<property name="password"><value>${config.jdbc.password}</value></property>
<property name="initialSize"><value>5</value></property>
<property name="maxActive"><value>100</value></property>
<property name="maxWait"><value>10000</value></property>
<property name="minIdle"><value>5</value></property>
<property name="validationQuery"><value>select 1 from dual</value></property>
</bean>
</beans>
classpath下共有两个文件一个是数据库配置,一个是参数配置这里主要说参数配置文件里面的参数的获取:
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import com.jiewen.commons.util.IOUtil; /** * 初始化加载密钥文件 * * @author P.M * */ public class LoadKeyFormProperties implements InitializingBean { private Resource keyFileResource; private static Map<String, String> map = new HashMap<String, String>(); protected static volatile boolean initialized = false; public static Map<String, String> getKey() { return map; } public void setKeyFileResource(Resource keyFileResource) { this.keyFileResource = keyFileResource; } /** * 将文件读取到map里面 */ private void loadKeyFormProperties() { if (initialized) { return; } InputStream is = null; try { is = keyFileResource.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = null; while ((str = br.readLine()) != null) { String[] data = str.split("="); map.put(data[0], data[1]); } initialized = true; } catch (Exception e) { } finally { IOUtil.closeQuietly(is); } } @Override public void afterPropertiesSet() throws Exception { loadKeyFormProperties(); } }