spring中读取properties文件数据,替换xml中${XXXX}。或者自定义类读取.properties文件
spring中读取properties文件数据,替换xml中${XXXX}。或者自定义方法读取1.PropertyPlaceholderConfigurer2.PropertyPlaceholderConfigurer作用3.在xml中引入多个外部**.properties文件4.**.properties中配置变量5.在其他xml中使用**.properties文件中配置的参数6.还可以通过来引
spring中读取properties文件数据,替换xml中${XXXX}。或者自定义方法读取
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得到所有配置文件参数集合
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)