1. 实例化Bean对象,这个时候Bean的对象是非常低级的,基本不能够被我们使用,因为连最基本的属性都没有设置,可以理解为连Autowired注解都是没有解析的;
2. 填充属性,当做完这一步,Bean对象基本是完整的了,可以理解为Autowired注解已经解析完毕,依赖注入完成了;
3. 如果Bean实现了BeanNameAware接口,则调用setBeanName方法;
4. 如果Bean实现了BeanClassLoaderAware接口,则调用setBeanClassLoader方法;
5. 如果Bean实现了BeanFactoryAware接口,则调用setBeanFactory方法;
6. 调用BeanPostProcessor的postProcessBeforeInitialization方法;
7. 如果Bean实现了InitializingBean接口,调用afterPropertiesSet方法;
8. 如果Bean定义了init-method方法,则调用Bean的init-method方法;
9. 调用BeanPostProcessor的postProcessAfterInitialization方法;当进行到这一步,Bean已经被准备就绪了,一直停留在应用的上下文中,直到被销毁
;
10. 如果应用的上下文被销毁了,如果Bean实现了DisposableBean接口,则调用destroy方法,如果Bean定义了destory-method声明了销毁方法也会被调用。
为了验证上面的逻辑,可以做个试验:
首先定义了一个Bean,里面有各种回调和钩子,其中需要注意下,我在SpringBean的构造方法中打印了studentService,看SpringBean被new的出来的时候,studentService是否被注入了;又在
setBeanName中打印了studentService,看此时studentService是否被注入了,以此来验证,Bean是何时完成的自动注入的:
1 public class SpringBean implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware,BeanClassLoaderAware {23 public SpringBean() {4 System.out.println("SpringBean构造方法:" + studentService);5 System.out.println("SpringBean构造方法");6 }78 @Autowired9 StudentServiceImpl studentService;1011 @Override12 public void afterPropertiesSet() throws Exception {13 System.out.println("afterPropertiesSet");14 }1516 @Override17 public void destroy() throws Exception {18 System.out.println("destroy");19 }2021 @Override22 public void setBeanClassLoader(ClassLoader classLoader) {23 System.out.println("setBeanClassLoader");24 }2526 @Override27 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {28 System.out.println("setBeanFactory");29 }3031 @Override32 public void setBeanName(String name) {33 System.out.println("setBeanName:" + studentService);34 System.out.println("setBeanName");35 }3637 public void initMethod() {38 System.out.println("initMethod");39 }4041 public void destroyMethod() {42 System.out.println("destroyMethod");43 }44 }
再定义一个BeanPostProcessor,在重写的两个方法中进行了判断,如果传进来的beanName是
springBean才进行打印:
1 @Component2 public class MyBeanPostProcessor implements BeanPostProcessor {3 @Override4 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {5 if(beanName.equals("springBean")) {6 System.out.println("postProcessBeforeInitialization");7 }8 return bean;9 }1011 @Override12 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {13 if(beanName.equals("springBean")) {14 System.out.println("postProcessAfterInitialization");15 }16 return bean;17 }18 }
定义一个配置类,完成自动扫描,但是SpringBean是手动注册的,并且声明了initMethod和
destroyMethod:
@Configuration2 @ComponentScan3 public class AppConfig {4 @Bean(initMethod = "initMethod",destroyMethod = "destroyMethod")5 public SpringBean springBean() {6 return new SpringBean();7 }8 }
最后就是启动类了:
1 public static void main(String[] args) {2 AnnotationConfigApplicationContext annotationConfigApplicationContext =3 new AnnotationConfigApplicationContext(AppConfig.class);4 annotationConfigApplicationContext.destroy();5 }
运行结果:
1 SpringBean构造方法:null2 SpringBean构造方法3 setBeanName:com.codebear.StudentServiceImpl@311905264 setBeanName5 setBeanClassLoader6 setBeanFactory7 postProcessBeforeInitialization8 afterPropertiesSet9 initMethod10 postProcessAfterInitialization11 destroy12 destroyMethod