首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Spring加载属性资料的扩展

2012-07-05 
Spring加载属性文件的扩展???? 在项目中一个属性文件配置信息,提供给数据连接信息和主机信息,数据连接信息

Spring加载属性文件的扩展

???? 在项目中一个属性文件配置信息,提供给数据连接信息和主机信息,数据连接信息提供给spring框架使用,主机信息提供应用程序使用。不想写两个配置文件,特意对spring的读取属性的文件进行扩展。

下面为样例代码:

package com.easyway.ext.properties;import java.util.HashMap;import java.util.HashSet;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;/** * 采用Spring的加载属性信息 * 在应用中获取 * @author longgangbai * */public class FilePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {  private Map<String, String> resolvedProps;//将属性保存起来  @Override  protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,      Properties props) throws BeansException {      super.processProperties(beanFactoryToProcess, props);      resolvedProps = new HashMap<String, String>();      for (Object key : props.keySet()) {          String keyStr = key.toString();          resolvedProps.put(keyStr, parseStringValue(props.getProperty(keyStr), props,                  new HashSet()));      }  }public Map<String, String> getResolvedProps() {return resolvedProps;}public void setResolvedProps(Map<String, String> resolvedProps) {this.resolvedProps = resolvedProps;}}

?

spring的配置 如下

<?xml version="1.0" encoding="UTF-8"?><beans  xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">      <bean id="propertyBean" name="code">package com.easyway.ext.properties;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import org.springframework.context.support.ClassPathXmlApplicationContext;/** *  * @author longgangbai * */public class PropertyBeanMain {public static void main(String[] args) {ClassPathXmlApplicationContext appctx=new ClassPathXmlApplicationContext("classpath:appContext.xml");FilePropertyPlaceholderConfigurer propertyBean=(FilePropertyPlaceholderConfigurer)appctx.getBean("propertyBean");Map<String,String> maps=propertyBean.getResolvedProps();Set<Entry<String,String>> entryset=maps.entrySet();for (Entry<String, String> entry : entryset) {System.out.println(entry.getKey()+" ="+entry.getValue());}}}

?

?

热点排行