您的位置:首页 > 编程语言 > Java开发

Spring PropertyPlaceholderConfigurer读取本地路径文件

2015-11-10 16:55 246 查看
方法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();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: