半夜思考之查漏补缺, Spring 中的容器后处理器

之前学 Spring 的时候 , 还没听过容器后处理器 , 但是一旦写出来 , 就会觉得似曾相识 .

容器配置器通常用于对 Spring 容器进行处理 , 并且总是在容器实例化任何其他 Bean 之前 , 读取配置文件中的元数据 , 并有可能修改这些数据 .

Spring 提供了如下几个常用的容器后处理器 :

  • PropertyPlaceholderConfigurer : 属性占位符配置器
  • PropertyOverrideConfigurer : 重写占位符配置器
  • CustomAutowireConfigurer : 自定义自动装配的配置器
  • CustomScopeConfigurer : 自定义作用域的配置器

1 . 属性占位符配置器

看到这个 PropertyPlaceholdConfigurer 时 , 我想大多数都会想到这个标签 :

<context:property-placeholder location="classpath:xx.properties">

没错 , 这个就是采用了 Scheme 风格之后的 PropertyPlaceholderConfigurer 的简化配置 , 下面还是写一下这个 Bean 后处理器的原始配置 :

<bean class="org.springframework.bean.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <!-- 以下是配置文件, 如果有多个, 可配置多个 -->
            <value>dbconn.properties</value>
        </list>
    </property>
</bean>

2 . 重写占位符配置器

重写占位符配置器的作用 :

按照第一个 , 这个重写占位符配置器也应该有 Scheme 形式的简化版 :

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1

原文地址:https://www.cnblogs.com/daimajun/p/7297459.html