spring加载properties属性文件到内存

本文介绍了一个自定义的Spring Bean工厂类CustomPropertyConfigurer,它通过加载配置文件jdbc.properties并处理其中的属性,将这些属性注入到Spring Bean中。通过applicationContext.xml配置文件实现了属性的加载和处理流程,最后在Main类中展示了如何获取并打印这些配置属性。

1. CustomPropertyConfigurer.java

package com.jdd.broker.admin.aspect;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;
 
public class CustomPropertyConfigurer extends PropertyPlaceholderConfigurer{
    private static Map<String,String> properties = new HashMap<String,String>();
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactoryToProcess,
            Properties props) throws BeansException {
        // cache the properties
        PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
                DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
        for(Entry<Object,Object> entry:props.entrySet()){
            String stringKey = String.valueOf(entry.getKey());
            String stringValue = String.valueOf(entry.getValue());
            stringValue = helper.replacePlaceholders(stringValue, props);
            properties.put(stringKey, stringValue);
        }
        super.processProperties(beanFactoryToProcess, props);
    }
     
    public static Map<String, String> getProperties() {
        return properties;
    }
     
    public static String getProperty(String key){
        return properties.get(key);
    }
}

 2. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
    default-lazy-init="true" default-autowire="byName" default-init-method="" default-destroy-method="">  
     
    <bean id="propertyConfigurer"  class="com.jdd.broker.admin.aspect.CustomPropertyConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:/jdbc.properties</value>  
            </list>  
        </property>  
    </bean>  
     
</beans>

 3. jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://rdsvl6l6emtdjrk6yi4k3public.mysql.rds.aliyuncs.com:3306/test_crowd?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
jdbc.username=test_crowd
jdbc.password=test_crowd

4. Main.java测试类

package com.jdd.broker.admin.aspect;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring-mvc.xml");
Map<String, String> properties = CustomPropertyConfigurer.getProperties();
System.out.println(properties);
}
}



转载于:https://my.oschina.net/messi31/blog/545550

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值