PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。这样的话,我只需要对properties文件进行修改,而不用对xml配置文件进行修改。
作用是什么呢?
有一些属性值不需要经常变更,但是有一些属性值可能随时改变,把经常会改动的属性值放在另一个文件中的话,程序使用起来也更方便。
下面将通过一个例子来理解PropertyPlaceholderConfigurer。
首先是配置文件,代码如下:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="propertyPlaceHolder.properties"/>
</bean>
<bean id ="student" class="co.jp.beanFactoryPost.Student">
<property name="name">
<value>${name}</value>
</property>
<property name="age">
<value>${age}</value>
</property>
<property name="birth">
<value>${birth}</value>
</property>
</bean>
</beans>
在这个配置文件中最重要的就是下面这段代码,
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="propertyPlaceHolder.properties"/>
</bean>
PropertyPlaceholderConfigurer会读取location所设定的属性文件,并将properties文件中的值,设定给${name},${age},${birth}从而完成依赖注入。
相应的properties文件的定义如下:
name=xiaohailin
age=27
birth=19820123
这里用到的student bean,的class的代码如下:
比较简单,主要是一个get,set方法
package co.jp.beanFactoryPost;
public class Student {
private String name;
private String age;
private String birth;
public void setName(String name)
{
this.name = name;
}
public void setAge(String age)
{
this.age = age;
}
public void setBirth(String birth)
{
this.birth = birth;
}
public String getName()
{
return this.name;
}
public String getAge()
{
return this.age;
}
public String getBirth()
{
return this.birth;
}
}
接着,写一个测试的主类,代码如下:
public class PropertyHolderPlaceDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("propertyPlaceHolder.xml");
Student student = (Student) ctx.getBean("student");
System.out.println(student.getAge());
System.out.println(student.getBirth());
System.out.println(student.getName());
}
}
分享到:
相关推荐
`PropertyPlaceholderConfigurer`是Spring提供的一个Bean工厂后处理器,它的主要任务是在Spring容器初始化Bean时,替换掉XML配置文件中所有`${...}`形式的占位符,将其替换为对应属性文件中的实际值。这使得我们可以...
PropertyPlaceholderConfigurer 的扩展应用 ----------------------------------- PropertyPlaceholderConfigurer 不仅可以用于加载 Properties 文件,还可以用于实现其他功能,例如属性文件加密解密等。在后续的...
将ZooKeeper与`PropertyPlaceholderConfigurer`结合,可以实现远程配置读取,使得系统能在运行时动态获取和更新配置,增强了系统的健壮性和扩展性。 实现这个功能,首先需要在ZooKeeper上创建一个节点,用于存放...
PropertyPlaceholderConfigurer的原理是,Spring容器初始化的时候,会读取xml或者annotation对Bean进行初始化。在初始化的时候,PropertyPlaceholderConfigurer会拦截Bean的初始化,并对配置的${pname}进行替换,...
`org.springframework.beans.factory.config.PropertyPlaceholderConfigurer` 是Spring框架中的一个重要组件,主要负责处理配置文件中的占位符替换。这个类是Spring在初始化bean时用来解析和注入环境变量或系统属性...
在Spring框架中,IoC(Inversion of Control)容器是核心组成部分,负责管理对象的生命周期和依赖关系。通常,我们会在应用内部的类路径(ClassPath)下放置配置文件,如`applicationContext.xml`,然后使用`...
以下将详细讲解这些知识点。 首先,我们来看如何拓展Spring的配置。在Spring中,我们通常会在XML配置文件中定义Bean及其属性。然而,为了更好地管理和组织配置,我们可以采用多种方法来扩展配置。例如,可以将...
4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:BeanPostProcessor 风格的Hello World.. 79 4.8.1.2 示例:RequiredAnnotationBeanPostProcessor 81 4.8.2 ...
下面我们将深入探讨Spring如何实现动态加载配置文件以及相关的知识点。 首先,Spring的核心是IoC(Inversion of Control)容器,它负责管理对象的生命周期和依赖关系。在初始化容器时,Spring会读取XML、Java配置或...
例如,AmlBeanFactory是最常用的实现之一,当客户端调用getBean()方法时,容器才会实例化对象,通常返回的是单例模式的实例。此外,ClassPathXmlApplicationContext是ApplicationContext的一个实现,它扩展了...
在Spring框架中,Bean的生命周期是其核心特性之一,它涉及到Bean从创建到销毁的整个过程。理解并掌握Bean的生命周期对于优化应用性能、管理和控制Bean的行为至关重要。 首先,我们来了解一下Spring容器如何创建Bean...
这一过程涉及到Spring的`PropertyPlaceholderConfigurer`、`BeanPostProcessor`、`InstantiationAwareBeanPostProcessor`等接口和类。 **工具的使用** Spring提供了多种方式来加载配置,例如XML配置文件、Java配置...
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
在Java开发领域,尤其是SSM(Spring、SpringMVC、MyBatis)框架的使用中,配置文件的管理是...通过`PropertyPlaceholderConfigurer`和`@Value`注解,我们可以轻松地管理配置信息,使项目结构更加清晰,易于扩展和维护。
在Spring框架中,Property Placeholder是用于管理应用配置属性的一个强大工具。这个例子“基于Spring 2.0 Property Placeholder配置的源码...这个例子是一个很好的学习起点,有助于深入理解Spring框架的核心功能之一。
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.1.1. 使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7....
Spring Framework是Java开发中的核心框架,以其灵活、可扩展性著称,广泛应用于企业级应用开发。本文将基于“spring-framework原始阅读笔记”展开,深入探讨Spring的核心组件,包括容器实现、属性名解析、AOP(面向...
例如,`PropertyPlaceholderConfigurer`就是在这里处理属性占位符的替换。 `registerBeanPostProcessors(beanFactory)`注册了BeanPostProcessors,这些处理器会在Bean初始化时介入,执行额外的操作,如AOP代理、...
IoC使得对象的创建和依赖关系的管理由Spring容器负责,而AOP则允许开发者分离关注点,如日志、事务管理等,使其独立于业务逻辑。 在实际应用中,首先需要将Spring的jar包导入项目中,然后创建一个名为`...