Spring框架中的Bean生命周期是指从创建到销毁的整个过程。在这个过程中,Spring容器会负责管理Bean的状态和行为。以下是Spring中Bean生命周期的详细解释:
-
实例化:Spring容器根据Bean的定义信息,通过反射机制创建Bean对象。
-
设置属性值:Spring容器会根据Bean定义中的配置信息,为Bean对象设置属性值。这包括依赖注入的过程。
-
调用BeanNameAware接口方法:如果Bean实现了BeanNameAware接口,Spring容器会调用setBeanName()方法,将Bean的名称传递给Bean。
-
调用BeanFactoryAware接口方法:如果Bean实现了BeanFactoryAware接口,Spring容器会调用setBeanFactory()方法,将BeanFactory实例传递给Bean。
-
调用ApplicationContextAware接口方法:如果Bean实现了ApplicationContextAware接口,Spring容器会调用setApplicationContext()方法,将ApplicationContext实例传递给Bean。
-
调用BeanPostProcessor的postProcessBeforeInitialization()方法:Spring容器会调用实现了BeanPostProcessor接口的类的postProcessBeforeInitialization()方法,对Bean进行预初始化处理。
-
调用InitializingBean接口方法:如果Bean实现了InitializingBean接口,Spring容器会调用afterPropertiesSet()方法,用于Bean的初始化操作。
-
调用自定义的初始化方法:如果在Bean定义中配置了init-method属性,Spring容器会调用指定的初始化方法。
-
调用BeanPostProcessor的postProcessAfterInitialization()方法:Spring容器会调用实现了BeanPostProcessor接口的类的postProcessAfterInitialization()方法,对Bean进行初始化后的处理。
-
Bean可以被使用:此时Bean已经初始化完成,可以被应用程序正常使用。
-
调用DisposableBean接口方法:当容器关闭时,如果Bean实现了DisposableBean接口,Spring容器会调用destroy()方法,用于Bean的资源释放操作。
-
调用自定义的销毁方法:如果在Bean定义中配置了destroy-method属性,Spring容器会调用指定的销毁方法。
-
销毁Bean:Spring容器销毁Bean对象,释放内存资源。
总结一下,Spring中Bean的生命周期包括实例化、设置属性值、调用Aware接口方法、调用BeanPostProcessor的前后处理方法、调用InitializingBean和DisposableBean接口方法以及自定义的初始化和销毁方法。在这个过程中,Spring容器负责管理Bean的状态和行为。
以下为示例代码
MyBean
package org.example.bean01.ch01;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class MyBean implements ApplicationContextAware, BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
private String name;
private String address;
private int phone;
private String beanName;
private BeanFactory beanFactory;
private ApplicationContext context;
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("调用<bean>的init-method属性指定的初始化方法");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
System.out.println("调用ApplicationContextAware.setApplicationContext接口");
}
public String show() {
return this.toString();
}
public MyBean() {
System.out.println("调用MyBean的构造器实例化");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("注入属性name");
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
System.out.println("注入属性address");
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
System.out.println("注入属性phone");
this.phone = phone;
}
@Override
public String toString() {
return "MyBean [address=" + address + ", name=" + name + ", phone=" + phone + "]";
}
// 这是BeanFactoryAware接口方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("调用BeanFactoryAware.setBeanFactory()");
this.beanFactory = arg0;
}
// 这是BeanNameAware接口方法
@Override
public void setBeanName(String arg0) {
System.out.println("调用BeanNameAware.setBeanName()");
this.beanName = arg0;
}
// 这是InitializingBean接口方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("调用InitializingBean.afterPropertiesSet()");
}
// 这是DiposibleBean接口方法
@Override
public void destroy() throws Exception {
System.out.println("调用DiposibleBean.destory()");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("调用<bean>的destroy-method属性指定的初始化方法");
}
}
MyBeanFactoryPostProcessor
package org.example.bean01.ch01;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class MyBean implements ApplicationContextAware, BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
private String name;
private String address;
private int phone;
private String beanName;
private BeanFactory beanFactory;
private ApplicationContext context;
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("调用<bean>的init-method属性指定的初始化方法");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
System.out.println("调用ApplicationContextAware.setApplicationContext接口");
}
public String show() {
return this.toString();
}
public MyBean() {
System.out.println("调用MyBean的构造器实例化");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("注入属性name");
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
System.out.println("注入属性address");
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
System.out.println("注入属性phone");
this.phone = phone;
}
@Override
public String toString() {
return "MyBean [address=" + address + ", name=" + name + ", phone=" + phone + "]";
}
// 这是BeanFactoryAware接口方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("调用BeanFactoryAware.setBeanFactory()");
this.beanFactory = arg0;
}
// 这是BeanNameAware接口方法
@Override
public void setBeanName(String arg0) {
System.out.println("调用BeanNameAware.setBeanName()");
this.beanName = arg0;
}
// 这是InitializingBean接口方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("调用InitializingBean.afterPropertiesSet()");
}
// 这是DiposibleBean接口方法
@Override
public void destroy() throws Exception {
System.out.println("调用DiposibleBean.destory()");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("调用<bean>的destroy-method属性指定的初始化方法");
}
}
MyBeanPostProcessor
package org.example.bean01.ch01;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
super();
System.out.println("调用BeanPostProcessor实现类构造器");
}
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!");
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!");
return arg0;
}
}
AppConfig
package org.example.bean01.ch01;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "org.example.bean01.ch01")
public class AppConfig {
@Bean
public MyBeanPostProcessor myBeanPostProcessor() {
return new MyBeanPostProcessor();
}
@Bean
public MyBeanFactoryPostProcessor myBeanFactoryPostProcessor() {
return new MyBeanFactoryPostProcessor();
}
@Bean(initMethod = "myInit", destroyMethod = "myDestory")
public MyBean myBean() {
return new MyBean();
}
}
启动类BeanDemo
package org.example.bean01.ch01;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BeanDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
// 使用Bean
System.out.println("Using bean: " + myBean.show());
context.close();
}
}