生命周期
[1]___Person()[2]___Person -- setName()[3]______BeanNameAware接口中的setBeanName被调用 = person[4]______ApplicationContextAware接口中的setApplicationContext被调用 = org.springframework.context.support.ClassPathXmlApplicationContext@35d176f7, started on Sun Nov 07 22:27:11 CST 2021[5]______BeanPostProcessor接口中的postProcessBeforeInitialization被调用 = person[6]______InitializingBean接口中的afterPropertiesSet被调用[7]______Person--XML指定的--init-method被调用[8]______BeanPostProcessor接口中的postProcessAfterInitialization被调用person[9]______Person--run()[10]______DisposableBean接口中的destroy()被调用[11]______Person--XML指定的--destroy-method被调用
测试类
package com.lff;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class Person implements BeanNameAware, ApplicationContextAware , InitializingBean , DisposableBean {private String name;public Person() {System.out.println("[1]___Person()");}public void setName(String name) {this.name = name;System.out.println("[2]___Person -- setName()");}/*** s为在xml配置Bean的id* */@Overridepublic void setBeanName(String s) {System.out.println("[3]______BeanNameAware接口中的setBeanName被调用 = " + s);}/*** 能够拿到容器对象,让我们知道我们是在哪个容器里面* */@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {System.out.println("[4]______ApplicationContextAware接口中的setApplicationContext被调用 = " + applicationContext);}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("[6]______InitializingBean接口中的afterPropertiesSet被调用" );}public void initMethod(){System.out.println("[7]______Person--XML指定的--init-method被调用");}public void run(){System.out.println("[9]______Person--run()");}@Overridepublic void destroy() throws Exception {System.out.println("[10]______DisposableBean接口中的destroy()被调用" );}public void destroyMethod(){System.out.println("[11]______Person--XML指定的--destroy-method被调用");}}
package com.lff;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.lang.Nullable;public class CommonBean implements BeanPostProcessor {@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("[5]______BeanPostProcessor接口中的postProcessBeforeInitialization被调用 = " + beanName);return bean;}@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("[8]______BeanPostProcessor接口中的postProcessAfterInitialization被调用" + beanName);return bean;}}
XML配置
<bean id="person" class="com.lff.Person" init-method="initMethod" destroy-method="destroyMethod"><property name="name" value="lifufa" /></bean><bean class="com.lff.CommonBean" />
详细解释
Bean完整的生命周期会经过很多方法的调用。划分为如下几类
- Bean自己的生命周期方法:构造方法、setter方法、通过配置文件指定的init-method、destroy-method的方法
BeanNameAware的接口
/*** s为在xml配置Bean的id* */@Overridepublic void setBeanName(String s) {System.out.println("[3]______BeanNameAware接口中的setBeanName被调用 = " + s);}
ApplicationContextAware的接口
/*** 能够拿到容器对象,让我们知道我们是在哪个容器里面* */@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {System.out.println("[4]______ApplicationContextAware接口中的setApplicationContext被调用 = " + applicationContext);}
InitializingBean接口
@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("[6]______InitializingBean接口中的afterPropertiesSet被调用" );}
DisposableBean接口
@Overridepublic void destroy() throws Exception {System.out.println("[10]______DisposableBean接口中的destroy()被调用" );}
BeanPostProcessor接口,可以做拦截器
@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("[5]______BeanPostProcessor接口中的postProcessBeforeInitialization被调用 = " + beanName);return bean;}@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("[8]______BeanPostProcessor接口中的postProcessAfterInitialization被调用" + beanName);return bean;}
