spring同时加载多个properties

java加载properties文件的方法

http://blog.csdn.net/u011063151/article/details/51888640

spring的xml加载properties文件

https://www.cnblogs.com/shanheyongmu/p/5806872.html

需要同时加载多个properties,上面基本没有借鉴的地方,重新写了一个

<!-- 属性文件获取 -->
    <bean id="propertyConfigurer" class="com.shiro.utils.ExtPropertyPlaceholderConfigurer">  
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath*:ha_redis.properties</value>
			</list>
		</property>
    </bean> 
bean的具体实现如下

package com.shiro.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * 扩展属性文件读取
 * 
 * @author 
 *
 */
public class ExtPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

	private static Map<String, Object> ctxPropertiesMap;

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

	public Object getContextProperty(String name) {
		return ctxPropertiesMap.get(name);
	}
}

这样就可以同时引用多个properties了
发布了64 篇原创文章 · 获赞 9 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/eadela/article/details/78794510