废话不多说,直接是上代码!
1.首先加载配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd ">
<task:annotation-driven/>
<!-- 加载配置文件 -->
<bean id="propertyConfigurer" class="com.zhrd.bussinss.platform.utils.CustomizedPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>/WEB-INF/system.properties</value>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>
<context:property-placeholder location="WEB-INF/system.properties" ignore-unresolvable="true" />
<context:property-placeholder location="WEB-INF/jdbc.properties" ignore-unresolvable="true" />
</beans>
2.工具类
package com.zhrd.bussinss.platform.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; public class CustomizedPropertyPlaceholderConfigurer 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 static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); } }
3.配置文件中信息
test=123456
4.应用工具类获取配置文件中信息
package com.zhrd.bussinss.platform.controller.rest; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.security.KeyStore; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.net.ssl.SSLContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContexts; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import com.zhrd.bussinss.platform.bo.PayHist; import com.zhrd.bussinss.platform.constants.PayHistoryPayStatus; import com.zhrd.bussinss.platform.constants.PayHistoryPayType; import com.zhrd.bussinss.platform.service.WeiXinPayService; import com.zhrd.bussinss.platform.utils.CustomizedPropertyPlaceholderConfigurer; import com.zhrd.bussinss.platform.utils.EncoderHandlerUtils; import com.zhrd.bussinss.platform.weixinPayUtils.ClientCustomSSL; @RestController @RequestMapping("/rest/weiXinPay") public class WeiXinPayRESTFULController { @Autowired WeiXinPayService weiXinPayService; /** * 获取配置文件信息 * @param request * @param response */ @RequestMapping(value="/getWeiXinUrl",method=RequestMethod.GET ) public void getUrl(HttpServletRequest request,HttpServletResponse response,@RequestParam String orderNo){ try{ CustomizedPropertyPlaceholderConfigurer.getContextProperty("test"); } } }