`
lgstarzkhl
  • 浏览: 330753 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

容器扩展点之PropertyPlaceholderConfigurer

阅读更多
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()); 
    }
}
分享到:
评论

相关推荐

    Spring属性占位符PropertyPlaceholderConfigurer的使用

    `PropertyPlaceholderConfigurer`是Spring提供的一个Bean工厂后处理器,它的主要任务是在Spring容器初始化Bean时,替换掉XML配置文件中所有`${...}`形式的占位符,将其替换为对应属性文件中的实际值。这使得我们可以...

    Spring中PropertyPlaceholderConfigurer的使用

    PropertyPlaceholderConfigurer 的扩展应用 ----------------------------------- PropertyPlaceholderConfigurer 不仅可以用于加载 Properties 文件,还可以用于实现其他功能,例如属性文件加密解密等。在后续的...

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

    将ZooKeeper与`PropertyPlaceholderConfigurer`结合,可以实现远程配置读取,使得系统能在运行时动态获取和更新配置,增强了系统的健壮性和扩展性。 实现这个功能,首先需要在ZooKeeper上创建一个节点,用于存放...

    Spring如何使用PropertyPlaceholderConfigurer读取文件

    PropertyPlaceholderConfigurer的原理是,Spring容器初始化的时候,会读取xml或者annotation对Bean进行初始化。在初始化的时候,PropertyPlaceholderConfigurer会拦截Bean的初始化,并对配置的${pname}进行替换,...

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    `org.springframework.beans.factory.config.PropertyPlaceholderConfigurer` 是Spring框架中的一个重要组件,主要负责处理配置文件中的占位符替换。这个类是Spring在初始化bean时用来解析和注入环境变量或系统属性...

    18 Spring IoC容器如何读取应用外部的xml,txt,图形或者属性文件?慕课专栏(1)1

    在Spring框架中,IoC(Inversion of Control)容器是核心组成部分,负责管理对象的生命周期和依赖关系。通常,我们会在应用内部的类路径(ClassPath)下放置配置文件,如`applicationContext.xml`,然后使用`...

    4.Spring应用扩展.pptx

    以下将详细讲解这些知识点。 首先,我们来看如何拓展Spring的配置。在Spring中,我们通常会在XML配置文件中定义Bean及其属性。然而,为了更好地管理和组织配置,我们可以采用多种方法来扩展配置。例如,可以将...

    Spring 3 Reference中文

    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如何实现动态加载配置文件以及相关的知识点。 首先,Spring的核心是IoC(Inversion of Control)容器,它负责管理对象的生命周期和依赖关系。在初始化容器时,Spring会读取XML、Java配置或...

    Spring 总结(1) 自用

    例如,AmlBeanFactory是最常用的实现之一,当客户端调用getBean()方法时,容器才会实例化对象,通常返回的是单例模式的实例。此外,ClassPathXmlApplicationContext是ApplicationContext的一个实现,它扩展了...

    Spring管理的Bean的生命周期

    在Spring框架中,Bean的生命周期是其核心特性之一,它涉及到Bean从创建到销毁的整个过程。理解并掌握Bean的生命周期对于优化应用性能、管理和控制Bean的行为至关重要。 首先,我们来了解一下Spring容器如何创建Bean...

    Spring_0200_IOC_Introduction setter注入

    这一过程涉及到Spring的`PropertyPlaceholderConfigurer`、`BeanPostProcessor`、`InstantiationAwareBeanPostProcessor`等接口和类。 **工具的使用** Spring提供了多种方式来加载配置,例如XML配置文件、Java配置...

    spring-property-annotations:Spring 2.5.x 组件的带注释的配置属性

    #Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...

    SSM 读取properties文件

    在Java开发领域,尤其是SSM(Spring、SpringMVC、MyBatis)框架的使用中,配置文件的管理是...通过`PropertyPlaceholderConfigurer`和`@Value`注解,我们可以轻松地管理配置信息,使项目结构更加清晰,易于扩展和维护。

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

    在Spring框架中,Property Placeholder是用于管理应用配置属性的一个强大工具。这个例子“基于Spring 2.0 Property Placeholder配置的源码...这个例子是一个很好的学习起点,有助于深入理解Spring框架的核心功能之一。

    Spring-Reference_zh_CN(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:spring-framework原始阅读笔记-spring源码阅读

    Spring Framework是Java开发中的核心框架,以其灵活、可扩展性著称,广泛应用于企业级应用开发。本文将基于“spring-framework原始阅读笔记”展开,深入探讨Spring的核心组件,包括容器实现、属性名解析、AOP(面向...

    Spring源码学习四:BeanDefinition装载前奏曲1

    例如,`PropertyPlaceholderConfigurer`就是在这里处理属性占位符的替换。 `registerBeanPostProcessors(beanFactory)`注册了BeanPostProcessors,这些处理器会在Bean初始化时介入,执行额外的操作,如AOP代理、...

    最新spring框架学习笔记(全)资料 (2).pdf

    IoC使得对象的创建和依赖关系的管理由Spring容器负责,而AOP则允许开发者分离关注点,如日志、事务管理等,使其独立于业务逻辑。 在实际应用中,首先需要将Spring的jar包导入项目中,然后创建一个名为`...

Global site tag (gtag.js) - Google Analytics