一、api分析
首先看下AutowireCapableBeanFactory
接口简单api,有下面接口可以简单的看到api中包含了创建bean,初始化,以及初始化前后的后置处理器api
初始化:
Object initializeBean(Object var1, String var2)
初始化前:
Object applyBeanPostProcessorsBeforeInitialization(Object var1, String var2)
初始化后:
Object applyBeanPostProcessorsAfterInitialization(Object var1, String var2)
public interface AutowireCapableBeanFactory extends BeanFactory {
int AUTOWIRE_NO = 0;
int AUTOWIRE_BY_NAME = 1;
int AUTOWIRE_BY_TYPE = 2;
int AUTOWIRE_CONSTRUCTOR = 3;
/** @deprecated */
@Deprecated
int AUTOWIRE_AUTODETECT = 4;
String ORIGINAL_INSTANCE_SUFFIX = ".ORIGINAL";
<T> T createBean(Class<T> var1) throws BeansException;
void autowireBean(Object var1) throws BeansException;
Object configureBean(Object var1, String var2) throws BeansException;
Object createBean(Class<?> var1, int var2, boolean var3) throws BeansException;
Object autowire(Class<?> var1, int var2, boolean var3) throws BeansException;
void autowireBeanProperties(Object var1, int var2, boolean var3) throws BeansException;
void applyBeanPropertyValues(Object var1, String var2) throws BeansException;
Object initializeBean(Object var1, String var2) throws BeansException;
Object applyBeanPostProcessorsBeforeInitialization(Object var1, String var2) throws BeansException;
Object applyBeanPostProcessorsAfterInitialization(Object var1, String var2) throws BeansException;
void destroyBean(Object var1);
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> var1) throws BeansException;
@Nullable
Object resolveDependency(DependencyDescriptor var1, @Nullable String var2) throws BeansException;
@Nullable
Object resolveDependency(DependencyDescriptor var1, @Nullable String var2, @Nullable Set<String> var3, @Nullable TypeConverter var4) throws BeansException;
}
2、具体执行过程
-
具体执行过程在
AbstractAutowireCapableBeanFactory
类内
在初始化方法内,会先后执行applyBeanPostProcessorsBeforeInitialization
和applyBeanPostProcessorsAfterInitialization
-
applyBeanPostProcessorsBeforeInitialization
和applyBeanPostProcessorsAfterInitialization
执行过程会获取容器中的BeanPostProcessor依次执行
-
实际初始化方法利用反射执行
初始化方法定义可以通过
生命周期-InitializingBean和DisposableBean
、生命周期-@PostConstruct&@PreDestroy