Spring PropertyPlaceholderConfigurer读取本地路径文件
2015-11-10 16:55
246 查看
方法1:
方法2:
<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(); } } } }
相关文章推荐
- Java 基本数据类型
- 用java编写的五只蚂蚁走出27厘米长的细目干的最大最小时间
- Java IO中的FileReader 和 FileWriter读写文件,和创建多级文件夹
- JAVA字典生成算法
- java的poi技术读取和写入excel
- spring scope prototype与singleton区别
- 【REST】REST、RESTFUL的理解以及SpringMVC实现Restful编程
- Java内存溢出及jvm内存设置
- 基于java的简单英雄联盟胜率计算
- Eclipse SVN修改用户名和密码
- 从”JAVA“而终 16:java sql学习资料大全
- java string
- java中的堆和栈
- 处理JSON的Java API :JSON的简介
- Spring的事务传播属性
- java源码分析04-FileInputStream
- Spring mvc 搭建与总结
- web项目在eclipse中服务器端无法识别
- Java 生成PDF方法总结
- java List 排序 Collections.sort()