Spring类中获取配置文件信息

本文详细介绍了如何在Spring Boot应用中加载配置文件,并通过自定义PropertyPlaceholderConfigurer实现复杂属性解析,包括处理未找到资源和无法解析占位符的情况。此外,还展示了如何在应用工具类中获取配置文件中的特定信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说,直接是上代码!

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");
		}	
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值