zoukankan      html  css  js  c++  java
  • 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进行处理;

            啦啦啦

  • 相关阅读:
    思源:秒级体验百亿级数据量监控钻取
    禧云Redis跨机房双向同步实践
    谈谈数据中台技术体系
    RCA:收单设备调用云端接口频繁超时排查总结
    技术上的“深淘滩,低作堰”
    企业私有源代码上传github致入侵之大疆案判决了
    那些年我们一起犯过的错
    异地双活的四个误区
    没有预见性你凭什么晋升
    中国IT史上两大严重事故对我们的警醒及预防措施
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6376078.html
Copyright © 2011-2022 走看看