`

Spring自定义PropertyPlaceholderConfigurer

阅读更多

1. CustomPropertyConfigurer.java

package propertyconfig;
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="propertyconfig.CustomPropertyConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:propertyconfig/project.properties</value>  
            </list>  
        </property>  
    </bean>  
</beans>  

 3. project.properties

site=iteye
blog=antlove
url=${site}/${blog}

 4. Main.java测试类

package propertyconfig;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	public static void main(String[] args) {
		new ClassPathXmlApplicationContext("propertyconfig/applicationContext.xml");
		
		Map<String,String> properties = CustomPropertyConfigurer.getProperties();
		
		System.out.println(properties);
	}
}

 

分享到:
评论

相关推荐

    Spring PropertyPlaceholderConfigurer配置文件加载器集成ZooKeeper来实现远程配置读取

    在Spring中,`PropertyPlaceholderConfigurer`是一个非常重要的类,它用于处理属性文件中的占位符,将它们替换为实际的值。这在配置管理中起到了关键作用,特别是在大型分布式系统中,动态配置管理变得尤为重要。...

    Spring如何使用PropertyPlaceholderConfigurer读取文件

    Spring如何使用...使用PropertyPlaceholderConfigurer可以将配置文件的路径放在java虚拟机JVM的自定义变量中,例如:-Ddev.config=/dev.properties,这样可以实现配置文件的路径根据环境的变化而变化。 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    PropertyPlaceholderConfigurer示例 3.7.2.2. PropertyOverrideConfigurer示例 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的...

    在Spring中使用加密外部属性文件

    Spring默认的`PropertyPlaceholderConfigurer`不直接支持加密的属性文件,但它提供了扩展点,允许我们自定义实现来处理加密后的属性。以下是一种实现方式: 1. 创建一个自定义的`PropertyPlaceholderConfigurer`...

    spring

    - **PropertyPlaceholderConfigurer** 和 **PropertyOverrideConfigurer**:用于在运行时动态替换Bean配置中的占位符。 ### 结论 Spring框架以其强大的功能和灵活性,成为Java企业级应用开发的首选框架之一。通过...

    Spring动态加载配置文件

    除了这两种方式,Spring Boot引入了更强大的`ConfigDataLocationResolver`和`ConfigDataLoader`接口,它们允许我们自定义配置数据的加载逻辑,支持更丰富的数据源,如Git、HTTP等。 在实际应用中,我们可能还需要...

    spring-reference

    Spring框架允许开发者自定义Bean类,并通过BeanDefinition将其配置到容器中。 ##### 3.2.4 Bean 标识符 (id 和 name) Bean的标识符用于唯一标识一个Bean。Spring框架允许使用id或name属性来为Bean指定标识符。 ###...

    springmvc+spring+mybatis

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置数据源 --&gt; ${driverClassName}"/&gt; ${url}"/&gt; ${username...

    Spring数据库连接等配置加密

    在IT行业中,尤其是在开发企业级应用时,...Spring提供了`PropertyPlaceholderConfigurer`和`EncryptablePropertyPlaceholderConfigurer`来实现这个功能。首先,我们需要创建一个解密器类,如下所示: ```xml ...

    spring工程需要的四个核心jar包之beans包

    - 在"beans"包中,`org.springframework.beans.factory.config`包下的`PropertyPlaceholderConfigurer`类用于处理占位符替换,实现环境变量或属性文件的值注入到Bean的属性中。 3. **Bean的生命周期管理** - ...

    Spring项目中怎么配置log4j

    在Spring项目中,我们可以使用Spring的`PropertyPlaceholderConfigurer`来加载外部化的log4j配置,这样在不同环境中可以使用不同的配置文件。在Spring的配置文件`applicationContext.xml`中添加以下代码: ```xml ...

    spring源码经典实例

    2. **配置文件改进**:将`jdbc.properties`移动到合适的位置,如`src/main/resources`,并使用Spring的`PropertyPlaceholderConfigurer`或者`@Value`注解读取配置,以实现数据库连接参数的动态加载。 3. **使用...

    spring-reference.pdf

    3. **Bean的初始化与销毁**:Spring支持自定义Bean的初始化和销毁方法,这些方法可以在Bean的生命周期中被自动调用,以执行必要的设置或清理工作。 六、Bean的属性与依赖关系 1. **属性设置**:可以通过setter方法...

    spring 启动时加载不同的文件

    - **自定义配置类**: 创建一个名为`GollfPropertyPlaceholderConfigurer`的类,继承自`PropertyPlaceholderConfigurer`。 - **重写processProperties方法**: - 在这个方法中,首先通过调用`setGollfPropFiles()`...

    spring.net中文手册在线版

    Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...

    第十九章 Spring Environment 抽象(Environment Abstraction)1

    在Spring 3.1之前,占位符处理主要由`PropertyPlaceholderConfigurer`完成,而在3.1及以后版本,这个功能被`PropertySourcesPlaceholderConfigurer`和`EmbeddedValueResolver`接口接管,使得处理更加灵活和高效。...

    掌握Spring设计模式:Java工程师必备指南

    此外,`BeanFactoryPostProcessor`接口允许自定义扩展,如`PropertyPlaceholderConfigurer`用于处理配置文件中的占位符。 2. **工厂方法**:Spring提供的`FactoryBean`接口是工厂方法模式的体现。它使得我们能够...

    基于Spring2.0 Property Placeholder配置的源码例子

    Spring 2.0引入了`PropertyPlaceholderConfigurer`类,它是一个Bean工厂后处理器,负责在Bean定义中替换以`${...}`形式的占位符为实际的属性值。这些属性通常来自一个或多个`.properties`文件,可以是classpath下的...

    Spring项目application.xml配置文件加解密

    `EncryptPropertyPlaceholderConfigurer.java`就是这样一个类,它扩展了Spring的`PropertyPlaceholderConfigurer`,并覆盖了`resolvePlaceholder`方法来处理加密的属性值。 ```java import org.springframework....

Global site tag (gtag.js) - Google Analytics