关于replacePlaceholders

现在还没有完全验证好,有空看看报错信息

https://www.cnblogs.com/fanguangdexiaoyuer/p/5788432.html

1.目录结构

  

 

2.

 1 package cn.caojun.properties;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 5 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 6 import org.springframework.util.PropertyPlaceholderHelper;
 7 import java.util.HashMap;
 8 import java.util.Map;
 9 import java.util.Properties;
10 
11 public class CustomPropertyConfigurer extends PropertyPlaceholderConfigurer {
12     private static Map<String,String> properties = new HashMap<String,String>();
13     protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
14     // cache the properties
15         PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
16                 DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
17         for(Map.Entry<Object,Object> entry:props.entrySet()){
18              String stringKey = String.valueOf(entry.getKey());
19              String stringValue = String.valueOf(entry.getValue());
20              //用属性文件键值属性props替换字符串stringValue
21              stringValue = helper.replacePlaceholders(stringValue, props);
22              properties.put(stringKey, stringValue);
23          }
24         super.processProperties(beanFactoryToProcess, props);
25     }
26 
27     public static Map<String, String> getProperties() {
28         return properties;
29     }
30 
31     public static String getProperty(String key){
32         return properties.get(key);
33     }
34 }

 

3.property

1 site=iteye
2 blog=antlove
3 url=${site}/${blog}

 

4.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     <bean id="SpringDemo" class="cn.caojun.spring.SpringDemo"></bean>
 6     <bean id="propertyConfigurer" class="cn.caojun.properties.CustomPropertyConfigurer">
 7         <property name="locations">
 8             <list>
 9                 <value>classpath:cn/caojun/properties/project.properties</value>
10             </list>
11         </property>
12     </bean>
13 </beans>

 

5.Main

 1 package cn.caojun.properties;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import java.util.Map;
 7 
 8 public class Main {
 9     public static void main(String[] args) {
10         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
11         CustomPropertyConfigurer customPropertyConfigurer=(CustomPropertyConfigurer)applicationContext.getBean("propertyConfigurer");
12         Map<String,String> properties = customPropertyConfigurer.getProperties();
13         System.out.println(properties);
14     }
15 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值