读取spring properties 文件属性
http://www.oschina.net/code/snippet_1760858_54923
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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: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-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="annotation.*" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- <context:property-placeholder location="classpath:/readProperties/jdbc.properties"/> --> <bean id="propertyConfigurer" class="readProperties.com.util.PropertyUtil"> <property name="location" value="classpath:/readProperties/jdbc.properties"/> </bean> </beans> <!-- 配置多个文件读取方式 --> <!-- <bean id="propertyConfigurer" class="com.common.util.PropertyUtil"> --> <!-- <property name="locations"> --> <!-- <list> --> <!-- <value>classpath*:/jdbc.properties</value> --> <!-- <value>classpath*:/url.properties</value> --> <!-- <value>classpath*:/sms.properties</value> --> <!-- <value>classpath*:/email.properties</value> --> <!-- </list> --> <!-- </property> --> <!-- </bean> --> |
jdbc.properties
1 2 3 4 5 6 7 8 9 10 |
Java代码 connection.driverclass=com.mysql.jdbc.Driver ##connection.url=jdbc\:mysql\://10.142.12.208\:3306/InvFinAdmin?useUnicode\=true&characterEncoding\=UTF-8 connection.url=jdbc\:mysql\://127.0.0.1\:3306/play?useUnicode\=true&characterEncoding\=UTF-8 connection.username=root ##connection.password=2a4c094bbb588e8169 connection.password=123456 connection.initialPoolSize=5 connection.minPoolSize=2 connection.maxPoolSize=10 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Java代码 package readProperties.com.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; public class PropertyUtil extends PropertyPlaceholderConfigurer{ public 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); } } |
Main-测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Java代码 package readProperties; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import readProperties.com.util.PropertyUtil; public class TestReadProperties { @Test public void testReadProperties(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:readProperties/read-properties-spring-config.xml"); System.out.println("--------------"); for(String key: PropertyUtil.ctxPropertiesMap.keySet()){ System.out.println("key=="+key+"---value:"+PropertyUtil.ctxPropertiesMap.get(key)); } } } |