两个接口 BeanPostProcessor ,InstantiationAwareBeanPostProcessor。
InstantiationAwareBeanPostProcessor是BeanPostProcessor
`public class MyInstantiationPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override<br /> **public **Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) **throws **BeansException {<br /> _/**<br /> * 执行时间在<br /> *<br /> * // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.<br /> * Object bean = resolveBeforeInstantiation(beanName, mbdToUse);<br /> *<br /> * Object beanInstance = doCreateBean(beanName, mbdToUse, args);//这里真正得创建bean<br /> * 这个方法里有 -》instanceWrapper = createBeanInstance(beanName, mbd, args); 创建bean(调用了构造函数)<br /> * -》populateBean(beanName, mbd, instanceWrapper); 属性赋值<br /> * -》exposedObject = initializeBean(beanName, exposedObject, mbd); //初始化<br /> *<br /> *<br /> *<br /> */
_System.**_out_**.println(**"postProcessBeforeInstantiation ------------------ InstantiationAwareBeanPostProcessor接口"**);<br /> **return null**;<br /> }
@Override<br /> **public boolean **postProcessAfterInstantiation(Object bean, String beanName) **throws **BeansException {
_/**<br /> * -》populateBean(beanName, mbd, instanceWrapper); 在 属性赋值一开始就执行了这个函数<br /> * // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the<br /> * // state of the bean before properties are set. This can be used, for example,<br /> * // to support styles of field injection.<br /> * if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {<br /> * for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {<br /> * if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {<br /> * return;<br /> * }* }<br /> * }<br /> *<br /> */
_System.**_out_**.println(**"postProcessAfterInstantiation ------------------ InstantiationAwareBeanPostProcessor接口"**);
**return false**;<br /> }
@Override<br /> **public **Object postProcessBeforeInitialization(Object bean, String beanName) **throws **BeansException {<br /> _/**<br /> * 在initializeBean 中得applyBeanPostProcessorsBeforeInitialization执行<br /> * Object beanInstance = doCreateBean(beanName, mbdToUse, args);//这里真正得创建bean<br /> * 这个方法里有 -》instanceWrapper = createBeanInstance(beanName, mbd, args); 创建bean(调用了构造函数)<br /> * -》populateBean(beanName, mbd, instanceWrapper); 属性赋值<br /> * -》exposedObject = initializeBean(beanName, exposedObject, mbd); //初始化<br /> * ->>> wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);<br /> * 这里遍历所有得BeanPostProcessors<br /> */
_System.**_out_**.println(**"postProcessBeforeInitialization ---------------------- BeanPostProcessor接口"**);<br /> **return null**;<br /> }
@Override<br /> **public **Object postProcessAfterInitialization(Object bean, String beanName) **throws **BeansException {<br /> _/**<br /> * 在initializeBean 中得applyBeanPostProcessorsBeforeInitialization执行<br /> * Object beanInstance = doCreateBean(beanName, mbdToUse, args);//这里真正得创建bean<br /> * 这个方法里有 -》instanceWrapper = createBeanInstance(beanName, mbd, args); 创建bean(调用了构造函数)<br /> * -》populateBean(beanName, mbd, instanceWrapper); 属性赋值<br /> * -》exposedObject = initializeBean(beanName, exposedObject, mbd); //初始化<br /> * ->>> wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);<br /> * 这里遍历所有得BeanPostProcessors<br /> *<br /> */<br /> _System.**_out_**.println(**"postProcessAfterInitialization ---------------------- BeanPostProcessor接口"**);
**return null**;<br /> }<br />}
`