zoukankan      html  css  js  c++  java
  • 关于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 }
  • 相关阅读:
    2019.7.17东湖大数据页面三
    2019.7.17东湖大数据页面二
    2019.7.17东湖大数据页面一
    css的使用方法和css选择器
    css定位和浮动
    form表单的理解及用法
    CSS中路径及form表单的用法
    关于新手html的认识 以及对table的基本用法
    网络编程和并发34题
    爬虫day01
  • 原文地址:https://www.cnblogs.com/juncaoit/p/8205319.html
Copyright © 2011-2022 走看看