Spring源码学习-PropertyPlaceholderHelper
2016-08-19 17:31
549 查看
转载:http://my.oschina.net/ydsakyclguozi/blog/465526
1. CustomPropertyConfigurer.java
package propertyconfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;
public class CustomPropertyConfigurer extends PropertyPlaceholderConfigurer{
private static Map<String,String> properties = new HashMap<String,String>();
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
// cache the properties
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
for(Entry<Object,Object> entry:props.entrySet()){
String stringKey = String.valueOf(entry.getKey());
String stringValue = String.valueOf(entry.getValue());
//用属性文件键值属性props替换字符串stringValue
stringValue = helper.replacePlaceholders(stringValue, props);
properties.put(stringKey, stringValue);
}
super.processProperties(beanFactoryToProcess, props);
}
public static Map<String, String> getProperties() {
return properties;
}
public static String getProperty(String key){
return properties.get(key);
}
}
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"
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" default-lazy-init="true" default-autowire="byName" default-init-method="" default-destroy-method="">
<bean id="propertyConfigurer" class="propertyconfig.CustomPropertyConfigurer">
<property name="locations">
<list>
<value>classpath:propertyconfig/project.properties</value>
</list>
</property>
</bean>
</beans>
3. project.properties
site=iteye
blog=antlove
url=${site}/${blog}
4. Main.java测试类
package propertyconfig;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("propertyconfig/applicationContext.xml");
Map<String,String> properties = CustomPropertyConfigurer.getProperties();
System.out.println(properties);
}
}
1. CustomPropertyConfigurer.java
package propertyconfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;
public class CustomPropertyConfigurer extends PropertyPlaceholderConfigurer{
private static Map<String,String> properties = new HashMap<String,String>();
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
// cache the properties
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
for(Entry<Object,Object> entry:props.entrySet()){
String stringKey = String.valueOf(entry.getKey());
String stringValue = String.valueOf(entry.getValue());
//用属性文件键值属性props替换字符串stringValue
stringValue = helper.replacePlaceholders(stringValue, props);
properties.put(stringKey, stringValue);
}
super.processProperties(beanFactoryToProcess, props);
}
public static Map<String, String> getProperties() {
return properties;
}
public static String getProperty(String key){
return properties.get(key);
}
}
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"
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" default-lazy-init="true" default-autowire="byName" default-init-method="" default-destroy-method="">
<bean id="propertyConfigurer" class="propertyconfig.CustomPropertyConfigurer">
<property name="locations">
<list>
<value>classpath:propertyconfig/project.properties</value>
</list>
</property>
</bean>
</beans>
3. project.properties
site=iteye
blog=antlove
url=${site}/${blog}
4. Main.java测试类
package propertyconfig;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("propertyconfig/applicationContext.xml");
Map<String,String> properties = CustomPropertyConfigurer.getProperties();
System.out.println(properties);
}
}
相关文章推荐
- Spring源码解析之PropertyPlaceholderHelper(占位符解析器)
- spring学习5.2-PropertyPlaceholderConfigurer 和PropertyOverrideConfigurer
- spring占位符解析器---PropertyPlaceholderHelper
- 【学习笔记】Spring的PropertyPlaceholderConfigurer应用
- spring学习的点点滴滴——PropertyPlaceholderConfigurer,PropertiesFactoryBean
- Spring技术内幕5——利用property-placeholder节点配置Bean属性源码解析
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- spring源码学习之六 </property-placeholder>元素处理过程
- spring之PropertyPlaceholderConfigurer学习
- spring占位符解析器---PropertyPlaceholderHelper
- spring3.2.3常用配置学习一【PropertyPlaceholderConfigurer】
- Spring 占位符解析器的作用 - PropertyPlaceholderHelper
- spring占位符解析器---PropertyPlaceholderHelper
- spring学习笔记之handler mapping源码解读
- spring property injection with property-placeholder or property-override
- Spring源码学习(二)------ AOP
- Spring 属性占位符配置器 PropertyPlaceholderConfigurer
- spring配置文件中的context:property-placeholder属性
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- Spring源码学习(一)------ IoC