spring PropertyPlaceholderConfigurer自定义属性

spring PropertyPlaceholderConfigurer自定义属性

需要继承spring的PropertyPlaceholderConfigurer类
java代码

public class ConfigInitLoadingConfigurer extends PropertyPlaceholderConfigurer {
    private static final Logger log = LoggerFactory.getLogger(ConfigInitLoadingConfigurer.class);
    private boolean override = true;
    private static String[] keyPatterns;
    private HessianConfigRetrieveServiceImp configRetrieveService = new HessianConfigRetrieveServiceImp();
    private HessianConfigRetrieveService selfConfigRetrieveService;
    private HessianAppRegisterService selfAppRegisterService;
    private HashMap<String, String> keyMappings = new HashMap();
    private List<String> prefixStripKeys = new ArrayList();
    private static String appName;
    private static String appEnv;

    public void setSelfConfigRetrieveService(HessianConfigRetrieveService selfConfigRetrieveService) {
        this.selfConfigRetrieveService = selfConfigRetrieveService;
    }

    public boolean isOverride() {
        return this.override;
    }

    public void setOverride(boolean override) {
        this.override = override;
    }

    public void setLocalStoreEnabled(boolean enabled) {
        DynCfgUtil.setLocalStoreEnabled(enabled);
    }

    public static String[] getKeyPatterns() {
        return keyPatterns;
    }

    public void setKeyPatterns(String[] keyPatterns) {
        keyPatterns = keyPatterns;
    }

    public HashMap<String, String> getKeyMappings() {
        return this.keyMappings;
    }

    public void setKeyMappings(HashMap<String, String> keyMappings) {
        this.keyMappings = keyMappings;
    }

    public List<String> getPrefixStripKeys() {
        return this.prefixStripKeys;
    }

    public void setPrefixStripKeys(List<String> prefixStripKeys) {
        this.prefixStripKeys = prefixStripKeys;
    }

    public static String getAppName() {
        if(StringUtils.isBlank(appName)) {
            appName = getAppNameByProperties();
            if(StringUtils.isBlank(appName)) {
                if(log.isErrorEnabled()) {
                    log.error("参数appName必须配置,不能为空");
                }

                throw new IllegalArgumentException("参数appName必须进行配置");
            }
        }

        log.info("current app name:" + appName);
        return appName;
    }

    public void setAppName(String appName) {
        log.info("setAppName name:" + appName);
        appName = appName;
    }

    public static String getAppEnv() {
        if(StringUtils.isBlank(appEnv)) {
            log.info("appEnv env is null");
            return null;
        } else {
            return appEnv;
        }
    }

    public void setAppEnv(String appEnv) {
        log.info("setAppEnv env:" + appEnv);
        appEnv = appEnv;
    }

    public void setSelfAppRegisterService(HessianAppRegisterService selfAppRegisterService) {
        this.selfAppRegisterService = selfAppRegisterService;
    }

    public ConfigInitLoadingConfigurer() {
    }
 }

spring 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
       default-lazy-init="true">
    <import resource="classpath*:/context/acms-client.xml" />
    <bean class="com.uns.inf.acms.client.config.ConfigInitLoadingConfigurer"
          parent="baseConfigurer" lazy-init="false">
        <property name="appName" value="app-test" />
        <property name="appEnv" value="${env}" />
        <property name="prefixStripKeys">
            <list>
                <value>cash.${env}</value>
            </list>
        </property>
        <property name="keyPatterns" value="XXXX,XXX.${env}" />
    </bean>
</beans>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值