Spring PropertyPlaceholderConfigurer读取本地路径文件

本文介绍了如何使用Spring的PropertyPlaceholderConfigurer组件从本地路径加载配置文件,详细讲解了具体的配置步骤和用法,帮助读者理解如何在Spring应用中动态读取属性文件。

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

方法1:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  <property name="locations">  
   <list>  
     <value>file:${user.dir}/config/init.properties</value>  
     <value>file:${user.dir}/config/init2.properties</value>  
   </list>  
  </property>  
 </bean>

方法2:

<bean id="propertyConfigurer" class="com.wanhang.ydsj.core.CustomerPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>


package com.wanhang.ydsj.core;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

public class CustomerPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

	private Logger logger = LoggerFactory.getLogger(CustomerPlaceholderConfigurer.class);

	private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
	private static final String XML_FILE_EXTENSION = ".xml";
	
	private static Map<String, Object> ctxPropertiesMap;

	@Override
	protected void loadProperties(Properties props) throws IOException {
		Resource location = getLocation();
		if(location == null) {
			super.loadProperties(props);
		}
		
		try {
			fillProperties(props, new EncodedResource(location, "UTF-8"),
					this.propertiesPersister);
		} catch (IOException ex) {
			throw ex;
		}
		
	}
	
	@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);
	}

	public Resource getLocation() {
		Resource location = null;
		String homePath = System.getProperty("user.home");
		String fileSeparator = System.getProperty("file.separator");

		String filePath = homePath + fileSeparator + "jdbc.properties";

		logger.info("从系统根目录下读取到的配置文件是:{} ", filePath);

		File file = new File(filePath);
		if (file.exists()) {
			location = new FileSystemResource(file);
		} else {
			logger.info("{}文件不存在。", filePath);
		}
		return location;
	}
	
	static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
			throws IOException {

		InputStream stream = null;
		Reader reader = null;
		try {
			String filename = resource.getResource().getFilename();
			if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
				stream = resource.getInputStream();
				persister.loadFromXml(props, stream);
			}
			else if (resource.requiresReader()) {
				reader = resource.getReader();
				persister.load(props, reader);
			}
			else {
				stream = resource.getInputStream();
				persister.load(props, stream);
			}
		}
		finally {
			if (stream != null) {
				stream.close();
			}
			if (reader != null) {
				reader.close();
			}
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值