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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>name.properties</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>birthday.properties</value>
</property>
</bean>
</beans>
birthday.properties属性文件
birthday=2012-12-12
name.properties属性文件
name=kid
SpringPropertyResourceReader.java
package utils;
import java.lang.reflect.Method;
import java.util.Properties;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.PropertyResourceConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.support.PropertiesLoaderSupport;
public class SpringPropertyResourceReader {
private static ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
private static AbstractApplicationContext abstractContext =
(AbstractApplicationContext) applicationContext;
private static Properties properties=new Properties();
static{
try{
// get the names of BeanFactoryPostProcessor
String[] postProcessorNames = abstractContext
.getBeanNamesForType(BeanFactoryPostProcessor.class,true,true);
for (String ppName : postProcessorNames) {
// get the specified BeanFactoryPostProcessor
BeanFactoryPostProcessor beanProcessor=
abstractContext.getBean(ppName, BeanFactoryPostProcessor.class);
// check whether the beanFactoryPostProcessor is
// instance of the PropertyResourceConfigurer
// if it is yes then do the process otherwise continue
if(beanProcessor instanceof PropertyResourceConfigurer){
PropertyResourceConfigurer propertyResourceConfigurer=
(PropertyResourceConfigurer) beanProcessor;
// get the method mergeProperties
// in class PropertiesLoaderSupport
Method mergeProperties=PropertiesLoaderSupport.class.
getDeclaredMethod("mergeProperties");
// get the props
mergeProperties.setAccessible(true);
Properties props=(Properties) mergeProperties.
invoke(propertyResourceConfigurer);
// get the method convertProperties
// in class PropertyResourceConfigurer
Method convertProperties=PropertyResourceConfigurer.class.
getDeclaredMethod("convertProperties", Properties.class);
// convert properties
convertProperties.setAccessible(true);
convertProperties.invoke(propertyResourceConfigurer, props);
properties.putAll(props);
}
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
public static String getProperty(String propertyName){
return properties.getProperty(propertyName);
}
}
测试代码Main.java
package main;
import utils.SpringPropertyResourceReader;
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(SpringPropertyResourceReader.getProperty("name"));
System.out.println(SpringPropertyResourceReader.getProperty("birthday"));
}
}
分享到:
相关推荐
Spring中PropertyPlaceholderConfigurer的使用
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 ...
12.4.Spring.NET中的Advisor 12.5.使用ProxyFactoryObject创建AOP代理 12.5.1.基本原理 12.5.2.ProxyFactoryObject的属性 12.5.3.代理接口 12.5.4.代理一个类 12.6.使用ProxyFactory类以编程方式创建AOP代理 12.7....
第一个 Struts 程式 在 Struts 中整合 Spring 第一个 JSF 程式 在 JSF 中整合 Spring <br> 其它 Spring 提供了简化且一致的方式,让您在使用一些 API 或服务时更加简单。 远程...
6 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 7 8 9 <value>/WEB-INF/jdbc.properties 10 11 12 13 14 它配置了以下功能: 读取...
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
Spring中的配置信息 <?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop=...
4.6.1.6 在非Web 应用中,优雅地关闭Spring IoC 容器 74 4.6.2 ApplicationContextAware 和BeanNameAware . 75 4.6.3 其它Aware 接口 75 4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1...
2.4 新的模块组织方式和系统构建方式............................................................................................... 23 2.5 新特性概述 .......................................................
JDBCTemp+MySQL示例。Spring,PropertyPlaceholderConfigurer,BeanPropertyRowMapper
什么 与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言... 首先,我们将使用PropertyPlaceholderConfigurer通过读取存储在.properties文件中的属性来
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>WEB-INF/jdbc.properties class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-...
spring3.2+strut2+hibernate4 注解方式。 spring.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
PropertyPlaceholderConfigurer PropertyResourceConfigurer PropertyValue PropertyValues PropertyValuesEditor PrototypeAspectInstanceFactory PrototypeTargetSource ProxyConfig ProxyFactory ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................