工具类
package com.wanggs.pay.payutil;
/**
* Created by Wgs on 2017/9/3.
*/
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import java.util.HashMap;
import java.util.Map;
/**
* 扩展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);
}
}
测试
package com.wanggs.controller;
import com.wanggs.pay.payutil.PropertyUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by Wgs on 2017/8/6.
*/
@Controller
@RequestMapping("/")
public class HelloController {
@Value("${wx.token}")
private String token;
@RequestMapping("/index")
public String index(){
System.out.println(token);
// PropertyPlaceholderConfigurer的用法
String result = PropertyUtil.getProperty("wx.token");
System.out.println(result);
return "hello";
}
}
springMvc 配置文件
<?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"
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">
<!--自动扫描@Controller-->
<context:component-scan base-package="com.wanggs.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--通过@Value注解读取.properties配置内容-->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<!--配置文件名称-->
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<!--PropertyPlaceholderConfigurer的配置-->
<bean id="PropertyUtil" class="com.wanggs.pay.payutil.PropertyUtil">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
</beans>