@Autowired 写在属性上,
首先根据类型查找,如果找到多个,再根据属性名字匹配
Bean的生命周期相关的接口方法:
postProcessBeforeInstantiation
实例化:constructor.newInstance(args..)
postProcessAfterInstantiation
postProcessBeforeInitialization
postProcessProperties
初始化:注解版的生命周期init回调@PostConstruct
InitializingBean#afterPropertiesSet() 接口版的生命周期初始化回调
自定义的生命周期初始化回调init-method="customInitMethod"
postProcessAfterInstantiation
销毁:
DisposableBean#destroy
相关接口:
1. InstantiationAwareBeanPostProcessor extends BeanPostProcessor
postProcessBeforeInstantiation :
Apply this BeanPostProcessor before the target bean gets instantiated. The returned bean object may be a proxy to use instead of the target bean, effectively suppressing default instantiation of the target bean.
postProcessAfterInstantiation:
Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.
This is the ideal callback for performing custom field injection on the given bean instance, right before Spring's autowiring kicks in.
postProcessProperties:
Post-process the given property values before the factory applies them to the given bean, without any need for property descriptors.
在工厂将给定的属性值应用于给定的bean之前对其进行后处理,而不需要任何属性描述符
2. BeanPostProcessor
postProcessBeforeInitialization
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.
postProcessAfterInitialization
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.
3. InitializingBean#afterPropertiesSet()
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
4. @PreDestroy DisposableBean#destroy 自定义的destroy方法
总结:
- BeanDefinition注册,形成 BeanDefinitionMap
- 遍历bdmap, 合并形成RootBeanDefinition,验证解析后根据名称和RootBeanDefinition解析出Class对象
- 实例化之前,判断有无InstantiationAwareBeanPostProcessor 的实例化前的处理
resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd)
postProcessBeforeInstantiation - 根据Class对象得到Constructor,通过反射实例化Bean对象
- 属性填充
populateBean(beanName, mbd, instanceWrapper);
- InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation
- InstantiationAwareBeanPostProcessor#postProcessProperties
- applyPropertyValues(beanName, mbd, bw, pvs);
- 初始化阶段
Aware回调处理:invokeAwareMethods(beanName, bean); 会执行BeanNameAware,BeanClassLoaderAware,BeanFactoryAware
初始化前:wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
初始化:invokeInitMethods(beanName, wrappedBean, mbd);
- ((InitializingBean) bean).afterPropertiesSet();接口版的生命周期init回调方法
- invokeCustomInitMethod(beanName, bean, mbd); 自定义的Bean生命周期初始化方法
初始化后: wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
这里 AOP对象的创建是通过AnnotationAwareAspectJAutoProxyCreator这个postprocessor处理的。
- 把初始化完成bean,放到 SingleObjects单例池中
=================
AutowiredAnnotationBeanPostProcessor
by default, Spring's @Autowired and @Value annotations.
CommonAnnotationBeanPostProcessor
@Resource @PostConstruct and @PreDestroy
依赖注入时,初始化前,销毁前