使用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- spring context配置文件 -->
<!-- 不要扫描app包下的Controller类 -->
<context:component-scan base-package="app">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<!-- spring的属性加载器,加载properties文件中的属性 -->
<bean id="propertyConfigurer"
class="component.common.utils.PropertyUtil">
<property name="locations">
<list>
<value>classpath:server.properties</value>
<value>classpath:weixin.properties</value>
</list>
</property>
</bean>
<bean class="component.mvc.Spring"/>
<import resource="classpath*:/spring/*.xml" />
</beans>
类
package component.common.utils;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 扩展PropertyPlaceholderConfigurer,方便读取属性<br>
* 使用方法:
* <pre>
* {@literal
* <!-- spring的属性加载器,加载properties文件中的属性 -->
* <bean id="propertyConfigurer"
* class="component.common.utils.PropertyUtil">
* <property name="location">
* <value>classpath:/config.properties</value>
* </property>
* </bean>
*
* 然后调用PropertyUtil.getProperty(key);即可读取属性文件中的内容了
*
* }
* </pre>
*
* @author tanghc
*/
public class PropertyUtil
extends PropertyPlaceholderConfigurer {
private static Map<String, String> propertiesData = new HashMap<String, String>();
@Override
protected String convertProperty(String propertyName, String propertyValue) {
String value = super.convertProperty(propertyName, propertyValue);
propertiesData.put(propertyName, value);
return value;
}
/**
* 获取Properties文件中的值,如果不存在则返回defaultValue
*
* @param key
* @param defaultValue
* @return
*/
public static String getProperty(String key, String defaultValue) {
String value = propertiesData.get(key);
return value == null ? defaultValue : value;
}
/**
* 获取Properties文件中的值,不存在返回null
*
* @param key
* @return
*/
public static String getProperty(String key) {
return getProperty(key, null);
}
}
(1)Controller 中 通过@value 读取properties配置信息
1、springmvc-servlet.xml 中配置:
<!--通过@Value注解读取.properties配置内容-->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<!--配置文件名称-->
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
2、basicmanagement.properties中配置参数
sessionTimeOut = 60
3、Controller 获取方式
@Value("#{configProperties['sessionTimeOut']}")
private int sessionTimeOut;
(二)Service 中 通过@value 读取properties配置信息
1、applicationContext.xml中的配置
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:basicmanagement.properties" />
</bean>
第二种方式
<!--读取properties配置文件-->
<context:property-placeholder location="classpath:config.properties"/>