欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PropertyPlaceholderConfigurer类重构

程序员文章站 2022-05-02 08:16:34
...

  

  在使用Spring框架的项目中,通过代码直接读取properties配置项不方便。为了更好读取,继承PropertyPlaceholderConfigurer类,重写processProperties()方法,将加载到Spring框架中的配置项缓存到一个Map中,同时对外提供接口方法进行调用,以达到目的,具体实现如下:

1、定义接口PropertiesConfig及方法 

import java.util.Properties;

public interface PropertiesConfig {

    String getValue(String key);

    String getValue(String key, String defaultValue);

    Properties getGroupProperties(String preFix, boolean retContainPrefix);

    // TODO 可 定义任意和properties相关的方法
}
2、继承PropertyPlaceholderConfigurer类,重写processProperties()方法,并实现PropertiesConfig接口
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

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 DefaultPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements PropertiesConfig {

    private Map<String, String> pMap = new HashMap<String, String>();

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        try {
            PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX,
                    DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
            Set<Entry<Object, Object>> pSet = props.entrySet();
            String key = null;
            String val = null;
            for (Entry<Object, Object> entry : pSet) {
                key = String.valueOf(entry.getKey());
                val = String.valueOf(entry.getValue());
                // 占位符替换为真实值
                val = helper.replacePlaceholders(val, props);

                // TODO 可做任意相关的处理,比如密码加解密等
                String lKey = key.toLowerCase();
                if (lKey.contains("pwd") || lKey.contains("password") || lKey.contains("passwd")
                        || lKey.contains("passw")) {
                    System.out.println("key:" + key + ",value:******");
                } else {
                    System.out.println("key:" + key + ",value:" + val);
                }
                pMap.put(key, val);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getValue(String key) {
        return pMap.get(key);
    }

    public String getValue(String key, String defaultValue) {
        String val = getValue(key);
        if (val == null) {
            return defaultValue;
        }
        return val;
    }

    public Properties getGroupProperties(String preFix, boolean retContainPrefix) {
        Properties pp = new Properties();
        if (preFix == null || preFix.trim().equals("")) {
            pp.putAll(pMap);
            return pp;
        }

        Set<Entry<String, String>> es = pMap.entrySet();
        String key = null;
        for (Entry<String, String> e : es) {
            key = e.getKey();
            if (key.startsWith(preFix)) {
                if (retContainPrefix) {
                    pp.put(key, e.getValue());
                } else {
                    pp.put(key.replaceFirst(preFix, ""), e.getValue());
                }
            }
        }
        return pp;
    }
}

3、修改springbean配置文件

  找到spring contentXML文件,将如下的修改为红色部分:

<!--<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->

<bean id="propertyConfigurer"

class="org.slive.spring.configurer.DefaultPropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:conf/xxx1.properties </value>

<value>classpath:conf/xxx2.properties </value>

</list>

</property>

</bean>


4、使用ProertiesConfig接口方法

(1) 任意bean类中注入

@Autowired

private ProertiesConfig config;


(2) 或者非bean类中,假设你项目中又一个SpringBeanUtil

private ProertiesConfig config =

SpringBeanUtil.getBean(ProertiesConfig.class);



相关标签: Spring properties