图解
![SpringBoot Bean Lifecycle](https://img-blog.csdnimg.cn/20210512005225796.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0phbWVzX0hsaA==,size_16,color_FFFFFF,t_70)
代码示例:
MyTestBean
package com.james.example;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
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;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class MyTestBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
InitializingBean, DisposableBean {
public MyTestBean() {
System.out.println("MyTestBean construct...");
}
@PostConstruct
public void postConstruct() {
System.out.println("MyTestBean postConstruct...");
}
@PreDestroy
public void preDestroy() {
System.out.println("MyTestBean preDestroy...");
}
public void initMethod() {
System.out.println("MyTestBean initMethod...");
}
public void destroyMethod() {
System.out.println("MyTestBean destroyMethod...");
}
@Override
public void destroy() throws Exception {
System.out.println("MyTestBean DisposableBean destroy...");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("MyTestBean InitializingBean afterPropertiesSet...");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("MyTestBean ApplicationContext applicationContext..." + applicationContext);
}
@Override
public void setBeanName(String beanName) {
System.out.println("MyTestBean BeanNameAware setBeanName..." + beanName);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("MyTestBean BeanFactoryAware setBeanFactory..." + beanFactory);
}
}
MyTestFactoryBean
package com.james.example;
import org.springframework.beans.factory.FactoryBean;
public class MyTestFactoryBean implements FactoryBean<MyTestFactoryBean> {
@Override
public MyTestFactoryBean getObject() throws Exception {
System.out.println("MyTestFactoryBean getObject...");
return new MyTestFactoryBean();
}
@Override
public Class<?> getObjectType() {
return MyTestFactoryBean.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
MyTestBeanBeanPostProcessor
package com.james.example;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyTestBeanBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MyTestBean) {
System.out.println("MyTestBean BeanPostProcessor postProcessBeforeInitialization...");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MyTestBean) {
System.out.println("MyTestBean BeanPostProcessor postProcessAfterInitialization...");
}
return bean;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="false">
<context:annotation-config/>
<context:component-scan base-package="com.james.example"/>
<bean id="myTestBean" class="com.james.example.MyTestBean" init-method="initMethod" destroy-method="destroyMethod"
factory-bean="myTestFactoryBean">
</bean>
<bean id="myTestFactoryBean" class="com.james.example.MyTestFactoryBean"></bean>
<bean id="myTestBeanBeanPostProcessor" class="com.james.example.MyTestBeanBeanPostProcessor"></bean>
</beans>
BeanLifecycleTest
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Object myTestBean = context.getBean("myTestBean");
System.out.println(myTestBean);
context.close();
输出结果
MyTestBean construct...
MyTestBean BeanNameAware setBeanName...myTestBean
MyTestBean BeanFactoryAware setBeanFactory...org.springframework.beans.factory.support.DefaultListableBeanFactory@23e028a9: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,myTestBean,myTestFactoryBean,myTestBeanBeanPostProcessor]; root of factory hierarchy
MyTestBean ApplicationContext applicationContext...org.springframework.context.support.ClassPathXmlApplicationContext@7106e68e
MyTestBean BeanPostProcessor postProcessBeforeInitialization...
MyTestBean postConstruct...
MyTestBean InitializingBean afterPropertiesSet...
MyTestBean initMethod...
MyTestBean BeanPostProcessor postProcessAfterInitialization...
com.james.example.MyTestBean@17695df3
MyTestBean preDestroy...
MyTestBean DisposableBean destroy...
MyTestBean destroyMethod...