8 -- 深入使用Spring -- 1...3 容器后处理器

      8.1.3 容器后处理器(BeanFactoryPostProcessor)

        容器后处理器负责处理容器本身。

        容器后处理器必须实现BeanFacotryPostProcessor接口。实现该接口必须实现如下一个方法:

        postProcessBeanFactory(ConfigurableListableBeanFacotry beanFactory) :该方法只是对Spring容器进行后处理,无须任何返回值。

        ApplicationContext可自动检测到容器中的容器后处理器,并自动注册容器后处理器。但若使用BeanFactory作为Spring容器,则必须手动调用该容器后处理器来处理BeanFactory容器。

        Class : MyBeanFactoryPostProcessor

package edu.pri.lime._8_1_3;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("程序对Spring所做的BeanFactory的初始化没有改变...");
        System.out.println("Spring容器是:" + beanFactory);
    }

}

        XML : 

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean class="edu.pri.lime._8_1_3.MyBeanFactoryPostProcessor" />
    
</beans>

        Class : BeanTest

package edu.pri.lime._8_1_3.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanTest {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("app_8_1_3.xml");
    }
}

        Console : 

二月 09, 2017 9:02:57 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2752f6e2: startup date [Thu Feb 09 21:02:57 CST 2017]; root of context hierarchy
二月 09, 2017 9:02:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [app_8_1_3.xml]
程序对Spring所做的BeanFactory的初始化没有改变...
Spring容器是:org.springframework.beans.factory.support.DefaultListableBeanFactory@3aefe5e5: defining beans [edu.pri.lime._8_1_3.MyBeanFactoryPostProcessor#0]; root of factory hierarchy

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

        ⊙ PropertyPlaceholderConfigurer : 属性占位符配置器。

        ⊙ PropertyOverrideConfigurer : 重写占位符配置器。

        ⊙ CustomAutowireConfigurer : 自定义自动装配的配置器。

        ⊙ CustomScopeConfigurer : 自定义作用域的配置器。

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

        如果有需要,程序可以配置多个容器后处理器,多个容器后处理器可设置order属性来控制容器后处理的执行次序。

        为了给容器后处理器指定order属性,则要求容器后处理器必须实现Ordered接口,因此在实现BeanFactoryPostProcessor时,就应当考虑实现Ordered接口。

        容器后处理器的作用域范围是容器级,它只对容器本身进行处理,而不对容器中的Bean进行处理;

        啦啦啦

转载于:https://www.cnblogs.com/ClassNotFoundException/p/6376078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值