05.Spring Framework 之扩展机制

1. BeanFactory 后置处理器

1.1 概述

BeanFactory 后置处理器有两种类型,分别是 BeanFactoryPostProcessor 和 BeanDefinitionRegistryPostProcessor

BeanFactoryPostProcessor 在 BeanFactory 标准初始化之后调用,用来定制和修改 BeanFactory 的内容,所有的 bean 定义已经保存加载到 beanFactory,但是 bean 的实例还未创建

BeanDefinitionRegistryPostProcessor 在所有 bean 定义信息将要被加载时触发,优先于 BeanFactoryPostProcessor 执行,利用 BeanDefinitionRegistryPostProcessor 可以给容器再额外添加一些组件

BeanDefinitionRegistry:Bean 定义信息的保存中心,以后 BeanFactory 就是按照 BeanDefinitionRegistry 里面保存的每一个 bean 定义信息创建 bean 实例

1.2 执行顺序

image

1.3 环境搭建

代码已经上传至 https://github.com/masteryourself-tutorial/tutorial-spring ,详见 tutorial-spring-framework/tutorial-spring-framework-beanfactorypostprocessor 工程

1. MyBeanDefinitionRegistryPostProcessor
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println(String.format(">>>>>>>>>>>>>>>> {%s} %s", "MyBeanDefinitionRegistryPostProcessor", "postProcessBeanFactory 执行了"));
        System.out.println(String.format(">>>>>>>>>>>>>>>> {%s} bean 的数量 %d", "MyBeanDefinitionRegistryPostProcessor", beanFactory.getBeanDefinitionCount()));
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println(String.format(">>>>>>>>>>>>>>>> {%s} %s", "MyBeanDefinitionRegistryPostProcessor", "postProcessBeanDefinitionRegistry 执行了"));
        System.out.println(String.format(">>>>>>>>>>>>>>>> {%s} bean 的数量 %d", "MyBeanDefinitionRegistryPostProcessor", registry.getBeanDefinitionCount()));
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Cat.class).getBeanDefinition();
        registry.registerBeanDefinition("hello", beanDefinition);
    }

}
2. MyBeanFactoryPostProcessor
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println(String.format("================ {%s} %s", "MyBeanFactoryPostProcessor", "postProcessBeanFactory 执行了"));
        System.out.println(String.format("================ {%s} bean 的数量是 %d", "MyBeanFactoryPostProcessor", beanFactory.getBeanDefinitionCount()));
    }

}
3. ExtensionApplication
public class ExtensionApplication {

    public static void main(String[] args) {
        // >>>>>>>>>>>>>>>> {MyBeanDefinitionRegistryPostProcessor} postProcessBeanDefinitionRegistry 执行了
        // >>>>>>>>>>>>>>>> {MyBeanDefinitionRegistryPostProcessor} bean 的数量 9
        // >>>>>>>>>>>>>>>> {MyBeanDefinitionRegistryPostProcessor} postProcessBeanFactory 执行了
        // >>>>>>>>>>>>>>>> {MyBeanDefinitionRegistryPostProcessor} bean 的数量 10
        // ================ {MyBeanFactoryPostProcessor} postProcessBeanFactory 执行了
        // ================ {MyBeanFactoryPostProcessor} bean 的数量是 10
        // person 构造函数执行了
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        context.close();
    }

}

2. EventListener 事件监听机制

2.1 使用方式

2.1.1 ApplicationListener

监听容器中发布的事件,事件驱动模型开发

监听 ApplicationEvent 及其子类的事件

2.1.2 @EventListener

可以使用注解简化开发,原理是通过 EventListenerMethodProcessor 处理器解析方法上的 @EventListener 注解

2.2 环境搭建

代码已经上传至 https://github.com/masteryourself-tutorial/tutorial-spring ,详见 tutorial-spring-framework/tutorial-spring-framework-listener 工程

1. MyApplicationListener
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("=============== MyApplicationListener 接收到事件:" + event.getClass());
    }

}
2. MyAnnotationListener
@Component
public class MyAnnotationListener {

    @EventListener(classes = {ApplicationEvent.class})
    public void applicationEventListen(ApplicationEvent event) {
        System.out.println("*************** MyAnnotationListener 接收到事件:" + event.getClass());
    }

}
3. ListenerApplication
public class ListenerApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        context.publishEvent(new ApplicationEvent("hello spring annotation") {
        });
        context.close();
        // *************** MyAnnotationListener 接收到事件:class org.springframework.context.event.ContextRefreshedEvent
        // =============== MyApplicationListener 接收到事件:class org.springframework.context.event.ContextRefreshedEvent
        // *************** MyAnnotationListener 接收到事件:class pers.masteryourself.tutorial.spring.framework.listener.ListenerApplication$1
        // =============== MyApplicationListener 接收到事件:class pers.masteryourself.tutorial.spring.framework.listener.ListenerApplication$1
        // *************** MyAnnotationListener 接收到事件:class org.springframework.context.event.ContextClosedEvent
        // =============== MyApplicationListener 接收到事件:class org.springframework.context.event.ContextClosedEvent
    }

}
发布了37 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/masteryourself/article/details/100640542
今日推荐