一、前面说了bean的容器初始化,后面当然是说bean的加载。这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉通过BeanFactory是怎么实现class的实例化的。

  二、声明一下:XmlBeanFactory和ApplicationContext的不同在与,ApplicationContext在容器初始化的时候,就做了很多准备操作。而XmlBeanFactory是在获取bean的时候才会调用初始化的东西。这一点是XmlBeanFactory做的不好的地方。比如:BeanPostProcessor、InitializingBean和标签init-method等,这些都是在bean加载的时候完成而不是在初始化的时候。

  三、bean的加载源码解读

  1)bean的获取方式

package com.pinnet.bean;

import com.pinnet.customLabel.User;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; public class Test { public static void main(String[] args) {
//读取资源
Resource resource = new ClassPathResource("spring-bean.xml");
//初始化容器
XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
//获取对应的bean
xmlBeanFactory.getBean("user", User.class);
}
}

  2)getBean

   public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return this.doGetBean(name, requiredType, (Object[])null, false);
}
protected <T> T doGetBean(String name, Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException {
//检查是否存在别名,采用存在别名的方式
final String beanName = this.transformedBeanName(name);
//通过单例的map来获取实例,当然这个第一次是没有的,是在加载过后放入到单例的map里面
Object sharedInstance = this.getSingleton(beanName);
Object bean;
if (sharedInstance != null && args == null) {
if (this.logger.isDebugEnabled()) {
if (this.isSingletonCurrentlyInCreation(beanName)) {
this.logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference");
} else {
this.logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
//如果存在bean的话,看是否是FactoryBean,如果是则通过factoryBean的方式加载
bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, (RootBeanDefinition)null);
} else {
if (this.isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
//如果存在父类bean的话,现价在父类
BeanFactory parentBeanFactory = this.getParentBeanFactory();
if (parentBeanFactory != null && !this.containsBeanDefinition(beanName)) {
String nameToLookup = this.originalBeanName(name);
if (args != null) {
return parentBeanFactory.getBean(nameToLookup, args);
} return parentBeanFactory.getBean(nameToLookup, requiredType);
}
if (!typeCheckOnly) {
this.markBeanAsCreated(beanName);
}
//获取在容器里面初始化的BeanDefinition
final RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
this.checkMergedBeanDefinition(mbd, beanName, args);
String[] dependsOn = mbd.getDependsOn();
String scopeName;
if (dependsOn != null) {
String[] var14 = dependsOn;
int var13 = dependsOn.length;
for(int var12 = 0; var12 < var13; ++var12) {
scopeName = var14[var12];
this.getBean(scopeName);
this.registerDependentBean(scopeName, beanName);
}
} //所有配置默认是为单例的
if (mbd.isSingleton()) {
sharedInstance = this.getSingleton(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
try {
//这里是整个bean加载的核心
return AbstractBeanFactory.this.createBean(beanName, mbd, args);
} catch (BeansException var2) {
AbstractBeanFactory.this.destroySingleton(beanName);
throw var2;
}
}
});
//如果存在bean的话,看是否是FactoryBean,如果是则通过factoryBean的方式加载
bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
} else if (mbd.isPrototype()) {
scopeName = null;
Object prototypeInstance;
try {
this.beforePrototypeCreation(beanName);
prototypeInstance = this.createBean(beanName, mbd, args);
} finally {
this.afterPrototypeCreation(beanName);
}
//如果存在bean的话,看是否是FactoryBean,如果是则通过factoryBean的方式加载
bean = this.getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
} else {
scopeName = mbd.getScope();
Scope scope = (Scope)this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
AbstractBeanFactory.this.beforePrototypeCreation(beanName);
Object var2;
try {
var2 = AbstractBeanFactory.this.createBean(beanName, mbd, args);
} finally {
AbstractBeanFactory.this.afterPrototypeCreation(beanName);
}
return var2;
}
});
bean = this.getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
} catch (IllegalStateException var19) {
throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; " + "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton", var19);
}
}
} if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
try {
return this.getTypeConverter().convertIfNecessary(bean, requiredType);
} catch (TypeMismatchException var20) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Failed to convert bean '" + name + "' to required type [" + ClassUtils.getQualifiedName(requiredType) + "]", var20);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
} else {
return bean;
}
}

  3)getSingleton、getMergedLocalBeanDefinition、createBean

  a、getSingleton

public Object getSingleton(String beanName) {
return this.getSingleton(beanName, true);
} protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//singletonObjects是一个map,主要是目的是存放单例bean
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
Map var4 = this.singletonObjects;
synchronized(this.singletonObjects) {
//如果singletonObjects没有,则在earlySingletonObjects去找
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject != NULL_OBJECT ? singletonObject : null;
}

  b、getMergedLocalBeanDefinition

   protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
//默认在mergedBeanDefinitions(map)里面查找
RootBeanDefinition mbd = (RootBeanDefinition)this.mergedBeanDefinitions.get(beanName);
//注意这里的getBeanDefinition,这里是从beanDefinitionMap里面取查找的,整个在容器加载的时候说明了
return mbd != null ? mbd : this.getMergedBeanDefinition(beanName, this.getBeanDefinition(beanName));
} protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd) throws BeanDefinitionStoreException {
return this.getMergedBeanDefinition(beanName, bd, (BeanDefinition)null);
} protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException {
Map var4 = this.mergedBeanDefinitions;
synchronized(this.mergedBeanDefinitions) {
RootBeanDefinition mbd = null;
if (containingBd == null) {
mbd = (RootBeanDefinition)this.mergedBeanDefinitions.get(beanName);
} if (mbd == null) {
if (bd.getParentName() == null) {
if (bd instanceof RootBeanDefinition) {
mbd = ((RootBeanDefinition)bd).cloneBeanDefinition();
} else {
//将其他类型BeanDefinition包装成RootBeanDefinition
mbd = new RootBeanDefinition(bd);
}
} else {
//父类bean不为空的操作
BeanDefinition pbd;
try {
String parentBeanName = this.transformedBeanName(bd.getParentName());
if (!beanName.equals(parentBeanName)) {
pbd = this.getMergedBeanDefinition(parentBeanName);
} else {
if (!(this.getParentBeanFactory() instanceof ConfigurableBeanFactory)) {
throw new NoSuchBeanDefinitionException(bd.getParentName(), "Parent name '" + bd.getParentName() + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent");
} pbd = ((ConfigurableBeanFactory)this.getParentBeanFactory()).getMergedBeanDefinition(parentBeanName);
}
} catch (NoSuchBeanDefinitionException var8) {
throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'", var8);
} mbd = new RootBeanDefinition(pbd);
mbd.overrideFrom(bd);
}
//这里默认设置成单例的
if (!StringUtils.hasLength(mbd.getScope())) {
mbd.setScope("singleton");
}
if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
mbd.setScope(containingBd.getScope());
}
if (containingBd == null && this.isCacheBeanMetadata() && this.isBeanEligibleForMetadataCaching(beanName)) {
this.mergedBeanDefinitions.put(beanName, mbd);
}
}
return mbd;
}
}
    public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
//不多解释,这里在容器初始化的时候进行操作
BeanDefinition bd = (BeanDefinition)this.beanDefinitionMap.get(beanName);
if (bd == null) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("No bean named '" + beanName + "' found in " + this);
} throw new NoSuchBeanDefinitionException(beanName);
} else {
return bd;
}
}

  c、getSingleton(这里实在调用createBean的地方)

public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
Assert.notNull(beanName, "'beanName' must not be null");
Map var3 = this.singletonObjects;
synchronized(this.singletonObjects) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
//创建bean之前做的操作
this.beforeSingletonCreation(beanName);
boolean recordSuppressedExceptions = this.suppressedExceptions == null;
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet();
}
try {
//这里调用createBean
singletonObject = singletonFactory.getObject();
} catch (BeanCreationException var13) {
BeanCreationException ex = var13;
if (recordSuppressedExceptions) {
Iterator var8 = this.suppressedExceptions.iterator(); while(var8.hasNext()) {
Exception suppressedException = (Exception)var8.next();
ex.addRelatedCause(suppressedException);
}
}
throw ex;
} finally {
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
this.afterSingletonCreation(beanName);
}
//添加到单例的map里面(这里不细讲解)
this.addSingleton(beanName, singletonObject);
}
return singletonObject != NULL_OBJECT ? singletonObject : null;
}
}

  d、createBean

    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Creating instance of bean '" + beanName + "'");
}
//实例化beanClass,放在mbd的beanClass下面,目前当做class被实例化在beanClass下
this.resolveBeanClass(mbd, beanName, new Class[0]);
try {
//准备重写的方法,不多介绍
mbd.prepareMethodOverrides();
} catch (BeanDefinitionValidationException var5) {
throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", var5);
}
Object beanInstance;
try {
//处理InstantiationAwareBeanPostProcessor下的调用,该结构继承BeanPostProcessor
beanInstance = this.resolveBeforeInstantiation(beanName, mbd);
if (beanInstance != null) {
return beanInstance;
}
} catch (Throwable var6) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", var6);
}
//这里主要是做一些初始化的操作等
beanInstance = this.doCreateBean(beanName, mbd, args);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}

  d.1 resolveBeanClass

    protected Class resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class... typesToMatch) throws CannotLoadBeanClassException {
try {
if (mbd.hasBeanClass()) {
return mbd.getBeanClass();
} else {
return System.getSecurityManager() != null ? (Class) AccessController.doPrivileged(new PrivilegedExceptionAction<Class>() {
public Class run() throws Exception {
return AbstractBeanFactory.this.doResolveBeanClass(mbd, typesToMatch);
}
//不存在beanClass的时候调用
}, this.getAccessControlContext()) : this.doResolveBeanClass(mbd, typesToMatch);
}
} catch (PrivilegedActionException var6) {
ClassNotFoundException ex = (ClassNotFoundException)var6.getException();
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
} catch (ClassNotFoundException var7) {
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), var7);
} catch (LinkageError var8) {
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), var8);
}
}
private Class doResolveBeanClass(RootBeanDefinition mbd, Class... typesToMatch) throws ClassNotFoundException {
if (!ObjectUtils.isEmpty(typesToMatch)) {
ClassLoader tempClassLoader = this.getTempClassLoader();
if (tempClassLoader != null) {
if (tempClassLoader instanceof DecoratingClassLoader) {
DecoratingClassLoader dcl = (DecoratingClassLoader)tempClassLoader;
Class[] var8 = typesToMatch;
int var7 = typesToMatch.length;
for(int var6 = 0; var6 < var7; ++var6) {
Class<?> typeToMatch = var8[var6];
dcl.excludeClass(typeToMatch.getName());
}
}
String className = mbd.getBeanClassName();
return className != null ? ClassUtils.forName(className, tempClassLoader) : null;
}
}
//这里应为typesToMatch不为空,所以走这里
return mbd.resolveBeanClass(this.getBeanClassLoader());
}
public Class resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException {
String className = this.getBeanClassName();
if (className == null) {
return null;
} else {
//通过反射创建实例
Class resolvedClass = ClassUtils.forName(className, classLoader);
this.beanClass = resolvedClass;
return resolvedClass;
}
}

  d.2 resolveBeforeInstantiation(不多解释)

    protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
if (mbd.hasBeanClass() && !mbd.isSynthetic() && this.hasInstantiationAwareBeanPostProcessors()) {
bean = this.applyBeanPostProcessorsBeforeInstantiation(mbd.getBeanClass(), beanName);
if (bean != null) {
bean = this.applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
} mbd.beforeInstantiationResolved = bean != null;
} return bean;
}

  d.3 doCreateBean

   protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, Object[] args) {
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = this.createBeanInstance(beanName, mbd, args);
}
final Object bean = instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null;
Class beanType = instanceWrapper != null ? instanceWrapper.getWrappedClass() : null;
Object var7 = mbd.postProcessingLock;
synchronized(mbd.postProcessingLock) {
if (!mbd.postProcessed) {
//这里的处理方式和c.2的处理方式类似
this.applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
mbd.postProcessed = true;
}
}
boolean earlySingletonExposure = mbd.isSingleton() && this.allowCircularReferences && this.isSingletonCurrentlyInCreation(beanName);
if (earlySingletonExposure) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references");
}
this.addSingletonFactory(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
return AbstractAutowireCapableBeanFactory.this.getEarlyBeanReference(beanName, mbd, bean);
}
});
}
Object exposedObject = bean;
try {
this.populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
//这里就是初始化的方法了
exposedObject = this.initializeBean(beanName, exposedObject, mbd);
}
} catch (Throwable var17) {
if (var17 instanceof BeanCreationException && beanName.equals(((BeanCreationException)var17).getBeanName())) {
throw (BeanCreationException)var17;
}
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", var17);
}
if (earlySingletonExposure) {
Object earlySingletonReference = this.getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
} else if (!this.allowRawInjectionDespiteWrapping && this.hasDependentBean(beanName)) {
String[] dependentBeans = this.getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet(dependentBeans.length);
String[] var15 = dependentBeans;
int var14 = dependentBeans.length;
for(int var13 = 0; var13 < var14; ++var13) {
String dependentBean = var15[var13];
if (!this.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.");
}
}
}
}
try {
this.registerDisposableBeanIfNecessary(beanName, bean, mbd);
return exposedObject;
} catch (BeanDefinitionValidationException var16) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", var16);
}
}
   protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
return null;
}
}, this.getAccessControlContext());
} else {
this.invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
//执行BeanPostProcessors的postProcessBeforeInitialization方法
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
}
try {
//执行实现InitializingBean,和init-method标签的内容
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
}
if (mbd == null || !mbd.isSynthetic()) {
//执行BeanPostProcessors的postProcessAfterInitialization方法
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
   public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Iterator var5 = this.getBeanPostProcessors().iterator();
while(var5.hasNext()) {
BeanPostProcessor beanProcessor = (BeanPostProcessor)var5.next();
result = beanProcessor.postProcessBeforeInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Iterator var5 = this.getBeanPostProcessors().iterator();
while(var5.hasNext()) {
BeanPostProcessor beanProcessor = (BeanPostProcessor)var5.next();
result = beanProcessor.postProcessAfterInitialization(result, beanName);
if (result == null) {
return result;
}
}
return result;
}
  protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
boolean isInitializingBean = bean instanceof InitializingBean;
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
((InitializingBean)bean).afterPropertiesSet();
return null;
}
}, this.getAccessControlContext());
} catch (PrivilegedActionException var6) {
throw var6.getException();
}
} else {
//调用InitializingBean接口的afterPropertiesSet方法,初始化
((InitializingBean)bean).afterPropertiesSet();
}
}
if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
if (initMethodName != null && (!isInitializingBean || !"afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) {
//执行init-method的方法,如果afterPropertiesSet执行出现异常,是不会执行init-method的方法的
this.invokeCustomInitMethod(beanName, bean, mbd);
}
}
} protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
String initMethodName = mbd.getInitMethodName();
final Method initMethod = mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName, new Class[0]) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName, new Class[0]);
if (initMethod == null) {
if (mbd.isEnforceInitMethod()) {
throw new BeanDefinitionValidationException("Couldn't find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'");
} else {
if (this.logger.isDebugEnabled()) {
this.logger.debug("No default init method named '" + initMethodName + "' found on bean with name '" + beanName + "'");
}
}
} else {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Invoking init method '" + initMethodName + "' on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
ReflectionUtils.makeAccessible(initMethod);
return null;
}
});
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
initMethod.invoke(bean);
return null;
}
}, this.getAccessControlContext());
} catch (PrivilegedActionException var9) {
InvocationTargetException ex = (InvocationTargetException)var9.getException();
throw ex.getTargetException();
}
} else {
try {
ReflectionUtils.makeAccessible(initMethod);
//执行方法
initMethod.invoke(bean);
} catch (InvocationTargetException var8) {
throw var8.getTargetException();
}
} }
}

  d.4 创建bean的过程结束了,这里主要的目的是初始化。

  四、bean的加载基本上就是这样了,不过还是梳理一下比较好

  1)通过getBean的方式进行,进行bean的获取

  2)bean的获取现从singletonObjects中获取,如果没有在进行createBean

  3)bean的获取是从beanDefinitionMap里面开始的,获取到对应的BeanDefinition,然后在进行处理

  4)createBean的过程中是实例化了bean并放入BeanDefinition的,然后进行了各种初始化

  5)在调用createBean的结束的时候在将bean放入了singletonObjects单例里面,这个实在源码解析c中描述的

  6)将获得bean进行返回,如果存在class类型,则适配返回,没有就object

  五、bean的加载过程基本上流程就这样,当然源码的解析过程,是比较痛苦的,但是在实现他的每一个实现都有他的意义。我这里只是更明显的阐述,实际的调用过程。当然这只是开始,后续的东西都是以此基础来进行拓展的。特别是在自定义标签环节。基本上拓展的新功能,都是在自定义标签下面实现的。

  六、bean的基本过程就是这样子了,然后就是后续的拓展功能,bean的增强化,还是自定义标签的使用上面,后续都会一一呈现,目的是更好的理解spring的拓展性和易用性

spring源码-bean之加载-2的更多相关文章

  1. Spring源码分析之-加载IOC容器

    本文接上一篇文章 SpringIOC 源码,控制反转前的处理(https://mp.weixin.qq.com/s/9RbVP2ZQVx9-vKngqndW1w) 继续进行下面的分析 首先贴出 Spr ...

  2. Mybatis源码解析(二) —— 加载 Configuration

    Mybatis源码解析(二) -- 加载 Configuration    正如上文所看到的 Configuration 对象保存了所有Mybatis的配置信息,也就是说mybatis-config. ...

  3. MyBatis 源码篇-资源加载

    本章主要描述 MyBatis 资源加载模块中的 ClassLoaderWrapper 类和 Java 加载配置文件的三种方式. ClassLoaderWrapper 上一章的案例,使用 org.apa ...

  4. 精尽Spring Boot源码分析 - 配置加载

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  5. spring源码-bean之增强初始化-3

    一.ApplicationContext的中文意思是“应用上下文”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持.资源访问(如URL和文件).事件传播 ...

  6. Spring 容器中bean的加载过程

    bean 的加载过程大致可以分为以下几个步骤: 1.获取配置的资源文件 2.对获取到的xml资源文件进行解析 3.获取包装资源 4.解析处理包装之后的资源 5.加载 提取bean 并进行注册(添加到b ...

  7. Spring Boot源码分析-配置文件加载原理

    在Spring Boot源码分析-启动过程中我们进行了启动源码的分析,大致了解了整个Spring Boot的启动过程,具体细节这里不再赘述,感兴趣的同学可以自行阅读.今天让我们继续阅读源码,了解配置文 ...

  8. spring源码-bean之初始化-1

    一.spring的IOC控制反转:控制反转——Spring通过一种称作控制反转(IOC)的技术促进了松耦合.当应用了IOC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查 ...

  9. Spring源码--Bean的管理总结(一)

    前奏 最近看了一系列解析spring管理Bean的源码的文章,在这里总结下,方便日后复盘.文章地址https://www.cnblogs.com/CodeBear/p/10336704.html sp ...

随机推荐

  1. Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)

    一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...

  2. CF Gym101933K King's Colors

    题目分析 题目要求在树上涂上恰好\(K\)种颜色的方案数. 设\(f(k)\)表示恰好涂上\(k\)种颜色的方案数(答案即为\(f(K)\)). 设\(g(k)\)表示至多涂上\(k\)种颜色的方案数 ...

  3. 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)

    链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...

  4. 关于<meta>的各种用处以及移动端的常见问题

    1.优先使用最新版本的IE和Chrome <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1& ...

  5. TunSafe使用分享

    体验还是很棒的,关于使用中遇到的一些问题在此分享下 官网打不开? 这个真的很不解,科学以后遇到的, 后来在手机上同样的环境测试可以打开 最后居然使用ie浏览器解决了,看来是国产浏览器在作怪 ie终于打 ...

  6. 面试准备——(三)Selenium面试题总结

    一.Selenium基本知识 1. 什么是Selenium? Selenium是浏览器自动化工具,主要用来Web的自动化测试,以及基于Web的任务管理自动化.它支持的语言有:python.Java.r ...

  7. MVC学习八:MVC View提交数据

    学习编程最主要的就是数据交互,MVC中数据交互是怎么样的呢? 1.Controller向View传输数据在http://www.cnblogs.com/WarBlog/p/7127574.html中有 ...

  8. PAT——1005. 继续(3n+1)猜想 (25)

    卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...

  9. Select Sort

    package com.syd.sort; /** * Description: * ClassName:SelectSort * Package:com.syd.sort * Date:2018/6 ...

  10. Swift_协议

    Swift_协议 点击查看源码 //协议 @objc protocol SomeProtocol:class { //class代表只用类才能实现这个协议 func test() //@objc:OC ...