1.Internally, the Spring Framework uses BeanPostProcessor
implementations to process anycallback interfaces it can find and call the appropriate methods. If you need customfeatures or other lifecycle behavior Spring does not offer out-of-the-box, you canimplement a BeanPostProcessor
yourself. For more information, seeContainer Extension Points.
2.Bean的生命周期管理实现InitialnizingBean, DisposableBean;
3.To interact with the container’s management of the bean lifecycle, you can implement theSpringInitializingBean
andDisposableBean
interfaces. The container callsafterPropertiesSet()
for the former anddestroy()
for the latter to allow the beanto perform certain actions upon initialization and destruction of your beans.
4 .InitializingBean替代方案
1.<bean name="initRedisConfig" class="com.pingan.pafa5.RedisClusterFactoryBeanOPti" init-method="init"></bean>
2.在RedisClusterFactoryBeanOPti类中初始化方法init方法上加上注解@Bean
5.DisposableBean的替代方案
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
cleanup方法上添加@Bean方法
6.BeanPostProcessor 后置处理器中定义了Bean初始化的前置方法和后置方法,xml配置后才能发现后置处理器,可以定义多个处理器,Spring会按照定义的顺序去执行;
7.BeanPostProceccerFactory
8.ApplicationListener类的使用
应用上下文事件处理器通常是通过ApplicationEven类和ApplicationListener interface,如果一个Bean实现了部署在context中的接口,那么每一次应用 事件发布在应用个环境中的时候,Bean会被通知,实质上,这是标准的观察者模式;
角色分工:应用事件 应用监听者 事件发布者
角色关联:1.自定义的事件(BlackListEvent)继承应用事件(ApplicationEvent),应用事件构造器初始化;
2.应用监听者实现ApplicationCationListener<自定义事件>,其中onApplicationEvent()为监听到事件后处理哪些逻辑;
3.事件的触发者要实现ApplicationPublisherAware方法,构造ApplicationPublisher去发布事件,满足特定的条件时就触发事件;
4.xml中要配置发布事件的Bean和初始化监听者的Bean;