在applicationContext加載的時候,已經将BeanDefition儲存在一個concurrentHashMap中,然而這些并不是真正的bean,真正的bean其實還沒有初始化以及依賴注入。對于整個依賴注入的過程,大緻可以分為兩個階段,
第一階段是Bean的初始化,如何采用設值注入的方式,那麼這個階段就是利用預設構造器去構造一個空對象,等待着注入;如果是構造注入,那麼這個過程可能就會觸發依賴注入,并遞歸地初始化依賴對象;
第二個階段是注入階段。
下面看看這個初始化階段觸發了哪些關鍵的方法:
Ioc容器的依賴注入發生的時機是不一樣的。對于Singleton的bean,是在refresh()的末尾階段進行了初始化和注入。對于其他scope的bean,他們的初始化發生在第一次getBean的時候。然後不管是何時發生這些動作,初始化的動作都有getBean方法發起。
1 Singleton的初始化開始
在refresh()方法快要結束的地方,調用了這樣一個方法finishBeanFactoryInitialization
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
從它的注釋就可以看到,這裡是用來裝載所有非延遲加載的單例(可以設定Bean的屬性 lazy-init=true)對象。從這裡開始了singleton對象的加載與注入。從這也可以看出,所有非延遲加載的singleton對象在applicationContext初始化完成的時候,就已經加載完畢。這個方法向下調用了DefaultListableBeanFactory中定義的preInstantiateSingletons方法。這個方法是用來初始化singleton對象的。 這個方法其實很簡單,周遊所有定義為singgleton的對象,然後調用getBean方法,完成初始化。然後調用定義的後處理方法,完成初始化。
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);
// 周遊所有的singleton對象 實際調用getBean方法
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return ((SmartFactoryBean<?>) factory).isEagerInit();
}
}, getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
else {
getBean(beanName);
}
}
}
// 注冊後處理方法...
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
smartSingleton.afterSingletonsInstantiated();
return null;
}
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
2 getBean()方法
從前面已經看到,即使是随applicationContext初始化過程啟動的singleton對象,它也利用getBean方法進行着初始化。可見所有Bean的生命周期都是由這裡開始的。
getBean方法很簡單,它向下調用了AbstractBeanFactroy類中定義的doGetBean方法。
3doGetBean方法
doGetBean方法有點類似于前面章節提到了refresh方法,它是整個依賴注入過程的入口,其實也定義了整個依賴注入過程都發生了哪些動作。
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
// 實際建立的Bean的包裝容器
BeanWrapper instanceWrapper = null;
//對于單例模式下的Bean,可能在建立過程中斷過,暫時放在緩存裡
if (mbd.isSingleton()) {
//從緩存中恢複未建立完成的Bean對象
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
//真正建立對象的入口
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
// Allow post-processors to modify the merged bean definition.
//Bean建立後處理
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
mbd.postProcessed = true;
}
}
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
// 解決循環引用的地方,當singleton遇到循環引用時,會放入singletonsCurrentlyInCreation中,暫停
// 在此處從singletonsCurrentlyInCreation中再拿到這個未完成初始化的對象
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName, new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
return getEarlyBeanReference(beanName, mbd, bean);
}
});
}
// 下面依賴注入
Object exposedObject = bean;
try {
//依賴注入的方法入口
populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
// 處理未完成注入的循環引用的bean
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// Register bean as disposable.
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
4 createBeanInstance 建立Bean的入口
在上面的“綱領性”的代碼塊中,可以看到建立Bean執行個體的入口是createBeanInstance。在這裡完成了對所有的Bean的執行個體化工作。它的整個過程如下:
這個過程主要完成了對一些用于執行個體化Bean的方法的調用。它主要分為四種情況
(1) 如果指定了工廠方法,向下調用instantiateUsingFactoryMethod方法執行個體化
(2)如果存在緩存過的構造器或者工廠方法,就使用這個緩存的方法進行執行個體化。在這個過程中,我們經常能看resolvedXXX,例如在源代碼裡有resolvedConstructorOrFactoryMethod,這是在RootBeanDefinition類中定義的一個屬性,類似的屬性還有很多。它是一種緩存機制,緩存了執行個體化的時候需要調用哪個構造方法或者工廠方法。這是非常有用的,對于一個類如果采用了構造注入或者自動裝配的方式,在初始化的時候需要對所有構造方法進行适配,這個搜尋的過程是耗時的,如果搜尋完的結果可以記錄下來,下一次直接利用,就減少了搜尋的時間。對于這種存在緩存的方法,根據類型選擇調用autowireConstructor或者instantiateBean進行執行個體化。
(3) 如果沒有緩存方法,就要選擇需要使用的構造器,如果存在适配的構造器,就向下調用autowireConstructor
(4) 否則使用無參的構造方法,向下調用instantiateBean。
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
//獲得需要建立的Bean的Class
Class<?> beanClass = resolveBeanClass(mbd, beanName);
// 确定需要建立的Bean的執行個體的類可以執行個體化
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
//如果存在工廠方法,則使用工廠方法去執行個體化類
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// 使用自動狀态的構造方式進行執行個體化
// 下面10行的代碼是在減少重複建立bean的工作量。如果已經建立過同樣的bean,直接使用上一次的配置屬性
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
//自動裝配構造執行個體
if (resolved) {
if (autowireNecessary) {
//傳回自動裝配的執行個體化結果
return autowireConstructor(beanName, mbd, null, null);
}
else {
//使用構造方法進行構造
return instantiateBean(beanName, mbd);
}
}
// 确定需要使用的構造方法
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
//自動裝配構造方法
return autowireConstructor(beanName, mbd, ctors, args);
}
// 使用預設的無參數的構造方法
return instantiateBean(beanName, mbd);
}
5autowireConstructor
隻有在配置檔案中指定了autowire的屬性時,才會觸發這個方法。這個方法實際上也是在搜尋構造器的過程,這裡面使用了貪心算法對構造器進行适配,而且僅僅去适配第一個找到的合适的方法。如果沒有找到,就是抛出異常,如果找到了,向下調用instantiate方法進入執行個體化階段。在整個搜尋過程,依然貫徹緩存的宗旨。查找都是從緩存中開始,搜尋結果存入緩存。
public BeanWrapper autowireConstructor(final String beanName, final RootBeanDefinition mbd,
Constructor<?>[] chosenCtors, final Object[] explicitArgs) {
//建構一個Bean的包裝容器
BeanWrapperImpl bw = new BeanWrapperImpl();
this.beanFactory.initBeanWrapper(bw);
// 搜尋已經使用過的構造器 節省時間
Constructor<?> constructorToUse = null;
ArgumentsHolder argsHolderToUse = null;
Object[] argsToUse = null;
if (explicitArgs != null) {
argsToUse = explicitArgs;
}
else {
Object[] argsToResolve = null;
synchronized (mbd.constructorArgumentLock) {
constructorToUse = (Constructor<?>) mbd.resolvedConstructorOrFactoryMethod;
if (constructorToUse != null && mbd.constructorArgumentsResolved) {
// Found a cached constructor...
argsToUse = mbd.resolvedConstructorArguments;
if (argsToUse == null) {
argsToResolve = mbd.preparedConstructorArguments;
}
}
}
if (argsToResolve != null) {
argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve);
}
}
//如果沒有使用過的構造器 利用貪心算法去搜尋合适的構造器
if (constructorToUse == null) {
// Need to resolve the constructor.
boolean autowiring = (chosenCtors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
ConstructorArgumentValues resolvedValues = null;
int minNrOfArgs;
if (explicitArgs != null) {
minNrOfArgs = explicitArgs.length;
}
else {
ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
resolvedValues = new ConstructorArgumentValues();
minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
}
// Take specified constructors, if any.
Constructor<?>[] candidates = chosenCtors;
if (candidates == null) {
Class<?> beanClass = mbd.getBeanClass();
try {
candidates = (mbd.isNonPublicAccessAllowed() ?
beanClass.getDeclaredConstructors() : beanClass.getConstructors());
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Resolution of declared constructors on bean Class [" + beanClass.getName() +
"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
}
}
AutowireUtils.sortConstructors(candidates);
int minTypeDiffWeight = Integer.MAX_VALUE;
Set<Constructor<?>> ambiguousConstructors = null;
LinkedList<UnsatisfiedDependencyException> causes = null;
for (int i = 0; i < candidates.length; i++) {
Constructor<?> candidate = candidates[i];
Class<?>[] paramTypes = candidate.getParameterTypes();
if (constructorToUse != null && argsToUse.length > paramTypes.length) {
// Already found greedy constructor that can be satisfied ->
// do not look any further, there are only less greedy constructors left.
break;
}
if (paramTypes.length < minNrOfArgs) {
continue;
}
ArgumentsHolder argsHolder;
if (resolvedValues != null) {
try {
String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, paramTypes.length);
if (paramNames == null) {
ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
if (pnd != null) {
paramNames = pnd.getParameterNames(candidate);
}
}
argsHolder = createArgumentArray(
beanName, mbd, resolvedValues, bw, paramTypes, paramNames, candidate, autowiring);
}
catch (UnsatisfiedDependencyException ex) {
if (this.beanFactory.logger.isTraceEnabled()) {
this.beanFactory.logger.trace(
"Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
}
// Swallow and try next constructor.
if (causes == null) {
causes = new LinkedList<UnsatisfiedDependencyException>();
}
causes.add(ex);
continue;
}
}
else {
// Explicit arguments given -> arguments length must match exactly.
if (paramTypes.length != explicitArgs.length) {
continue;
}
argsHolder = new ArgumentsHolder(explicitArgs);
}
int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
// Choose this constructor if it represents the closest match.
if (typeDiffWeight < minTypeDiffWeight) {
constructorToUse = candidate;
argsHolderToUse = argsHolder;
argsToUse = argsHolder.arguments;
minTypeDiffWeight = typeDiffWeight;
ambiguousConstructors = null;
}
else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
if (ambiguousConstructors == null) {
ambiguousConstructors = new LinkedHashSet<Constructor<?>>();
ambiguousConstructors.add(constructorToUse);
}
ambiguousConstructors.add(candidate);
}
}
//如果依然沒有找到合适的 抛出異常
if (constructorToUse == null) {
if (causes != null) {
UnsatisfiedDependencyException ex = causes.removeLast();
for (Exception cause : causes) {
this.beanFactory.onSuppressedException(cause);
}
throw ex;
}
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Could not resolve matching constructor " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
}
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Ambiguous constructor matches found in bean '" + beanName + "' " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
ambiguousConstructors);
}
if (explicitArgs == null) {
argsHolderToUse.storeCache(mbd, constructorToUse);
}
}
//利用找到的構造器去構造對象 依然使用getInstantiationStrategy().instantiate構造
try {
Object beanInstance;
if (System.getSecurityManager() != null) {
final Constructor<?> ctorToUse = constructorToUse;
final Object[] argumentsToUse = argsToUse;
beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
return beanFactory.getInstantiationStrategy().instantiate(
mbd, beanName, beanFactory, ctorToUse, argumentsToUse);
}
}, beanFactory.getAccessControlContext());
}
else {
beanInstance = this.beanFactory.getInstantiationStrategy().instantiate(
mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
}
bw.setWrappedInstance(beanInstance);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean instantiation via constructor failed", ex);
}
}
6 instantiateBean 完成Bean的初始化
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
try {
Object beanInstance;//建構出來的Bean執行個體
final BeanFactory parent = this;
if (System.getSecurityManager() != null) {
// 提升權限
beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
//調用具體執行個體化方法
return getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
}, getAccessControlContext());
}
else {
//調用具體執行個體化方法
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
//包裝建立好的執行個體
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
}
}
在這個方法裡面,向下調用了instantiate方法。他是執行個體化Bean的終點。在這裡決定了使用JDK的反射方法還是使用CGLIB的方法進行初始化。
public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner) {
// 這個if-else 把bean的初始化分為兩類
//如果定義了look-up method 或者replace method 将使用CGLIB初始化
//否則使用JDK的反射
if (bd.getMethodOverrides().isEmpty()) {
Constructor<?> constructorToUse;//獲得構造方法
synchronized (bd.constructorArgumentLock) {
//使用工廠方法 或者 構造器
constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
if (constructorToUse == null) {
final Class<?> clazz = bd.getBeanClass();
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
try {
if (System.getSecurityManager() != null) {
constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>() {
@Override
public Constructor<?> run() throws Exception {
return clazz.getDeclaredConstructor((Class[]) null);
}
});
}
else {
constructorToUse = clazz.getDeclaredConstructor((Class[]) null);
}
bd.resolvedConstructorOrFactoryMethod = constructorToUse;
}
catch (Exception ex) {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
}
return BeanUtils.instantiateClass(constructorToUse);
}
else {
// 使用CGLIB初始化
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
這裡根據是否定義了Override方法進行選擇,這裡的Override方法指的是lookupmethod和replacemethod
6.1 JDK反射生成Bean執行個體
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
Assert.notNull(ctor, "Constructor must not be null");
try {
//獲得通路權限
ReflectionUtils.makeAccessible(ctor);
//利用反射生成執行個體
return ctor.newInstance(args);
}
catch (InstantiationException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Is it an abstract class?", ex);
}
catch (IllegalAccessException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Is the constructor accessible?", ex);
}
catch (IllegalArgumentException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Illegal arguments for constructor", ex);
}
catch (InvocationTargetException ex) {
throw new BeanInstantiationException(ctor.getDeclaringClass(),
"Constructor threw exception", ex.getTargetException());
}
}
6.2 CGLIB執行個體化Bean
public Object instantiate(Constructor<?> ctor, Object... args) {
//利用CGLIB生成Class對象
Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
Object instance;
if (ctor == null) {
//利用反射對CGLIB生成的Class對象執行個體化
instance = BeanUtils.instantiate(subclass);
}
else {
try {
Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
//利用CGLIB進行執行個體化
instance = enhancedSubclassConstructor.newInstance(args);
}
catch (Exception ex) {
throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
}
}
// SPR-10785: set callbacks directly on the instance instead of in the
// enhanced class (via the Enhancer) in order to avoid memory leaks.
//在此處設定lookup和replacemethod
Factory factory = (Factory) instance;
factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
return instance;
}