spring中读取properties文件数据,替换xml中${XXXX}。或者自定义类读取.properties文件

1.PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。

2.PropertyPlaceholderConfigurer作用

在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改

3.在xml中引入多个外部**.properties文件

通过 <property><list><value>**.properties</value></list></property>配置多个properties。bean标签里class引入org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:env/dev/dev_****local.properties</value>
				<value>classpath*:env/dev/dev_**.properties</value>
			</list>
		</property>
		<property name="fileEncoding" value="UTF-8"></property>
	</bean>

4. **.properties中配置变量

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=-1
redis.testOnBorrow=true
redis.longTimeout=120
redis.shortTimeout=60

5.在其他xml中使用**.properties文件中配置的参数

<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxActive}"></property>
		<property name="maxIdle" value="${redis.maxIdle}"></property>
		<property name="maxWaitMillis" value="${redis.maxWait}"></property>
		<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
	</bean>

6.还可以通过<context:property-placeholder location=“classpath:***.properties” />来引入properties文件,但context标签最多只能写一个

<context:property-placeholder location="classpath*:**.properties"/>

注意:如果想引入多个属性文件,可以使用通配符:<context:property-placeholder location="classpath*:dev_**.properties"/>

7.除了在xml中引用还可以自定义PropertyPlaceholderConfigurer在代码中读取配置

我们通过继承PropertyPlaceholderConfigurer来编写自己的类把配置加载导内存中以便使用

package com.dubbo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author YangLeduo(养乐多)
 * @version 2021.05.25
 * @since jdk_1.8.0_271
 */
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {

    private static Map<String, String> ctxPropMap;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        ctxPropMap = new HashMap<>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = String.valueOf(props.get(key));
            ctxPropMap.put(keyStr,value);
        }

    }

    public static String getCtxProp(String name){
        return ctxPropMap.get(name);
    }

    public static Map<String, String> getCtxPropMap(){
        return ctxPropMap;
    }
}

定义完自己的类在配置文件中就不能引用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer这个类了。要改成自己的类com.dubbo.CustomizedPropertyConfigurer

<bean
class="com.dubbo.CustomizedPropertyConfigurer">
		<!--class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
		<property name="locations">
			<list>
				<value>classpath*:env/dev/dev_****local.properties</value>
				<value>classpath*:env/dev/dev_**.properties</value>
			</list>
		</property>
		<property name="fileEncoding" value="UTF-8"></property>
	</bean>

调用的时候直接get就行CustomizedPropertyConfigurer.getCtxProp("redis.maxActive")或者getCtxPropMap得到所有配置文件参数集合

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值