具体流程
bean的生命周期分4个阶段:
1.实例化
2.属性赋值
3.初始化
4.销毁
实例化就是在内存中new()出一个对象,属性赋值就是给那些被@Autowired修饰的属性注入对象,销毁是在Spring容器关闭时触发,初始化的步骤比较多:
1.是否实现Aware的相关接口,调用重载方法
(BeanNameAware#setBeanName,
BeanClassLoaderAware#setBeanClassLoader,
BeanFactoryAware#setBeanFactory,
ApplicationContextAware#setApplicationContext)
2.是否有bean实现BeanPostProcessor接口,调用实现的postProcessBeforeInitialization方法(实现类加载时不调用自身实现的方法,调用其他实现类的)
3.是否实现InitializingBean接口,调用重载方法afterPropertiesSet方法
4.是否指定@Bean的initMethod属性,调用指定的方法(定义在bean对应的类里,不是配置类里)
5.是否有bean实现BeanPostProcessor接口,调用实现的postProcessAfterInitialization方法(和第2点同理)
话不多说,下面用代码来给出示例。
代码示例
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.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class A implements BeanNameAware, BeanFactoryAware,
ApplicationContextAware, InitializingBean {
public A() {
System.out.println("a的构造函数");
}
@Override
public void setBeanName(String s) {
System.out.println("初始化-setBeanName");
}
@Override
public void setBeanFactory(BeanFactory beanFactory)
throws BeansException {
System.out.println("初始化-setBeanFactory");
}
@Override
public void setApplicationContext(ApplicationContext
applicationContext) throws BeansException {
System.out.println("初始化-setApplicationContext");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("初始化-afterPropertiesSet");
}
public void init() {
System.out.println("初始化-init");
}
}
import org.springframework.context.annotation.Bean;
public class MyConfiguration {
@Bean(initMethod = "init")
public A a() {
return new A();
}
}
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Import;
@Import(MyConfiguration.class)
public class BootStrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(BootStrap.class);
A bean = context.getBean(A.class);
}
}
运行启动类后,控制台打印:
这里单独再说下BeanPostProcessor,A类去实现它:
public class A implements BeanNameAware, BeanFactoryAware,
ApplicationContextAware, InitializingBean, BeanPostProcessor{
//...
//其余的方法不动,就添加下面2个方法
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
System.out.println(beanName + ":初始化-beforeInitialization");
return bean;
}
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
System.out.println(beanName + ":初始化-afterInitialization");
return bean;
}
}
再次运行启动类,控制台和之前的一样(A这个bean的初始化,没有调用实现的那2个方法):
A类还原,不实现BeanPostProcessor;MyConfiguration类实现该接口:
import org.springframework.context.annotation.Bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyConfiguration implements BeanPostProcessor {
@Bean(initMethod = "init")
public A a() {
return new A();
}
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
System.out.println(beanName + ":初始化-beforeInitialization");
return bean;
}
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
System.out.println(beanName + ":初始化-afterInitialization");
return bean;
}
}
再次运行启动类,这时想要的效果有了:
所以bean初始化中,不会调用自身实现BeanPostProcessor接口的那2个方法,调用其他实现BeanPostProcessor接口的bean的。