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进行处理;

            啦啦啦

  • 相关阅读:
    基于shell脚本比较数字加减乘除 要bc计算器
    基于shell脚本比较数字大小
    备份WordPress
    在CentOS 7 安装没有mysql
    SQL中判断字符串中包含字符的方法
    ASP.NET生成的HTML代码
    win7禁用休眠,献给c盘空间不足的朋友.
    SQLServer2005和2008的分页技术比较[转]
    浏览器兼容性系列--浅谈window.attachEvent
    在ASP.NET 的服务器端控件中有三种关于 ID 的属性
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6376078.html
Copyright © 2011-2022 走看看