3.Bean的实例化以及注入过程分析 Bean的实例以及注入是在getBean时触发的,由于外部容器是与外部调用交互的桥梁,我们首先从外部容器入手,AbstractApplicationContext类实现了getBean方法,代码如下:
AbstractApplicationContext下的代码:
public Object getBean(String name) throws BeansException {
//Bean的获取外部容器交给了内部容器
return getBeanFactory().getBean(name);
}
内部容器由DefaultListableBeanFactory承当,但真实的getBean方法实现是由其父类AbstractBeanFactory实现的sssssssssssssssssssssss。
AbstractBeanFactory下的代码:
public Object getBean(String name, Class requiredType, Object[] args) throws BeansException {
return doGetBean(name, requiredType, args, false);
}
3.1 doGetBean方法分析
protected Object doGetBean(
final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException {
//把当前传入的beanName转化为标准beanName,例如把去除&引用(如果带&表示要获取FactoryBean)、alias转化为标准的beanName等
final String beanName = transformedBeanName(name);
Object bean = null;
// Eagerly check singleton cache for manually registered singletons.
//先从singleton对象缓存中查找,如果未找到接着到早期的singleton对象缓存查找,如果还是没有找到,接下来先获得beanName的singletonFactory,然后调用getObject获得对象。
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null) {
/**//*获得给定bean实例的对象。这里的处理过程是这样的:
(1)如果sharedInstance不是FactoryBean实例,而是个普通bean,那么直接返回;
(2)如果sharedInstance是FactoryBean实例,且name要求返回FactoryBean实例,那么直接返回;
(3)否则从本地缓存获得合并BeanDefinition,如果本地缓存不存在合并BeanDefinition,则从本地缓存取得BeanDefinition,构造合并BeanDefinition,然后通过factoryBean获得bean*/
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}else {
//检查beanName所代表的bean是否处于正在创建阶段,主要是防止循环引用
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
//首先检查父工厂是否存在,如果父工厂存在,且当前工厂不存在该beanName的BeanDefinition,那么当前工厂将委托父工厂完成getBean的任务. BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// 转化为标准的beanName
String nameToLookup = originalBeanName(name);
if (args != null) {
// Delegation to parent with explicit args.
return parentBeanFactory.getBean(nameToLookup, args);
}
else {
//从父工厂获得bean,过程与本方法类似,相当于递归
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
if (!typeCheckOnly) {
//是否要类型检查,如果不检查,直接把beanName标记为已创建
markBeanAsCreated(beanName);
}
/**//*通过beanName,从mergedBeanDefinitions(针对mergeBeanDefinition的缓存)中取出合并的BeanDefinition,mergedBeanDefinition主要指两个BeanDefinition的合并,比如父子两个的合并*/
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {//如果bean存在依赖bean,先构建依赖bean
for (int i = 0; i < dependsOn.length; i++) {
String dependsOnBean = dependsOn[i];
getBean(dependsOnBean);
registerDependentBean(dependsOnBean, beanName);
}
}
// Create bean instance.
if (mbd.isSingleton()) {//
sharedInstance = getSingleton(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
try {
/**//*所有生成Bean都有BeanWrapper封装,bean的生成采用策略模式,CglibSubclassingInstantiationStrategy实现是默认的策略
1.如果配置为工厂方法创建(配置了factory-mother的),由java反射Mehthod的invoke完成bean的创建。
2.如果采用构造子配置创建(配置了constructor-arg的),如果bean对应的类包含多个构造子,采用cglib动态字节码构造;如果只有唯一的构造子,那么采用java反射Constructor的newInstance方法
3.如果是普通bean配置,直接通过反射Class默认的Constructor,然后调用newInstance获得bean.*/
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there.
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
}
});
/**//*由于当前构造的bean可能是FactoryBean,所以要通过以下方法获得真实需要的bean
(1)如果sharedInstance不是FactoryBean实例,而是个普通bean,那么直接返回;
(2)如果sharedInstance是FactoryBean实例,且name要求返回FactoryBean实例,那么直接返回;
(3)否则从本地缓存获得合并BeanDefinition,如果本地缓存不存在合并BeanDefinition,则从本地缓存取得BeanDefinition,构造合并BeanDefinition,然后通过factoryBean获得bean*/
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);//维护prototypesCurrentlyInCreation,主要把beanName加入到其中
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}else {
String scopeName = mbd.getScope();
final 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 {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
}
});
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
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",
ex);
}
}
}
// Check if required type matches the type of the actual bean instance.
if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return bean;
}
3.1.1 getObjectForBeanInstance方法分析
该方法从名称上就能看出,其实现的功能是从一个给定的bean实例,获取客户端调用所真实想要的bean。由于Spring能够管理各式的bean,当bean是FactoryBean实例的时候,客户端调用可以通过&+beanName的方式获得FactoryBean实例,而如果bean实例是普通的bean,那么该方法直接返回。
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {
/**//*如果bean是工厂解除参照(即name带有&),且不是FactoryBean实例,那么抛出异常。这里的意思就是说如果name带有&,而传进来的又不是FactoryBean实例*/
if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
}
/**//*现在我们拥有了bean实例,它可能是一个普通bean或者一个FactoryBean.如果是一个工厂bean,我们使用它创建一个bean实例,除非调用者确实想要工厂的引用。
如果bean实例未实现FactoryBean,或者要求返回BeanFactory自身,那么直接返回。*/
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance;
}
/**//*以下的代码意味着处理非普通bean,而是FactoryBean之类的*/
Object object = null;
if (mbd == null) {//
/**//*这里主要是从一个FactoryBean到object映射的缓存中,取出由FactoryBean创建的object.这种映射关系是由factoryBeanObjectCache(映射关系:FactoryBean name --> object ,例如’myJndi’--->myJndiObject)字段维护,该字段位于FactoryBeanRegistrySupport中*/
object = getCachedObjectForFactoryBean(beanName);
}
if (object == null) {
FactoryBean factory = (FactoryBean) beanInstance;
/**//*containsBeanDefinition方法是由默认内部工厂DefaultListableBeanFactory实现,因为BeanDefinition是缓存在它之下的*/
if (mbd == null && containsBeanDefinition(beanName)) {
/**//*从本地获得指定beanName的合并BeanDefinition,所谓的合并BeanDefinition就是指子类的BeanDefinition与父类BeanDefinition的合并。*/
mbd = getMergedLocalBeanDefinition(beanName);
}
/**//*判断beanDefinition是否是合成的,即是否与其他beanDefinition进行了合并*/
boolean synthetic = (mbd != null && mbd.isSynthetic());
//从factoryBean获得bean
object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
return object;
}
3.1.1.1 合并BeanDefinition获取的分析
在容器初始化阶段,对解释资源获得的BeanDefinition是由GenericBeanDefinition来定义,其只能刻画bean自身的一些特性。但bean与bean之间还存在着继承、方法覆盖、包含等一些复杂的关系。而这些关系的刻画是由RootBeanDefinition来负责的。RootBeanDefinition也就是我们下面所说的mergedBeanDefinition,mergedBeanDefinition是指对具有继承关系的bean所对应的BeanDefinition的合成,以达到完成表达一个bean的目的。
mergedBeanDefinition在获得后也将被缓存,其被缓存在mergedBeanDefinitions字段中。
protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
// Quick check on the concurrent map first, with minimal locking.
RootBeanDefinition mbd = (RootBeanDefinition) this.mergedBeanDefinitions.get(beanName);
if (mbd != null) {
return mbd;
}
return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
}
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
throws BeanDefinitionStoreException {
return getMergedBeanDefinition(beanName, bd, null);
}
/**//*以下是真正的合并BeanDefinition处理*/
protected RootBeanDefinition getMergedBeanDefinition(
String beanName, BeanDefinition bd, BeanDefinition containingBd)
throws BeanDefinitionStoreException {
synchronized (this.mergedBeanDefinitions) {//同步mergedBeanDefinitions
RootBeanDefinition mbd = null;
/**//*如果containingBd为空,先从缓存中查找,containingBd代表的包含bean的beanDefinition,那么什么是包含bean呢?比如B是A的内部类,那么A的实例就是B的实例的包含bean,这里的containingBd所代表的就是那个A。*/
if (containingBd == null) {
//这里说明了合并BeanDefinition的处理顺序是先里后外。
mbd =
(RootBeanDefinition) this.mergedBeanDefinitions.get(beanName);
}
if (mbd == null) {//如果缓存中不存在
if (bd.getParentName() == null) {//且无父类
//如果当前BeanDefinition不存在parentName,说明该类不存在父类,直接把该类对应的BeanDefinition拷贝到合并BeanDefinition
mbd = new RootBeanDefinition(bd);
}
else {
BeanDefinition pbd = null;
try {
String parentBeanName = transformedBeanName(bd.getParentName());//对beanName进行标准化
if (!beanName.equals(parentBeanName)) {//如果父类的名称与当前类的名称不相等
//获得父类的合并BeanDefinition
//这里说明了合并BeanDefinition的处理顺序是先父后子
pbd = getMergedBeanDefinition(parentBeanName);
}
else {
if (getParentBeanFactory() instanceof ConfigurableBeanFactory) {//如果相等,则从父工厂中获取
pbd = ((ConfigurableBeanFactory) getParentBeanFactory()).getMergedBeanDefinition(parentBeanName);
}
else {
throw new NoSuchBeanDefinitionException(bd.getParentName(),
"Parent name '" + bd.getParentName() + "' is equal to bean name '" + beanName +
"': cannot be resolved without an AbstractBeanFactory parent");
}
}
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,
"Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
}
//进行深度复制
mbd = new RootBeanDefinition(pbd);
//用子的BeanDefinition覆盖父亲的
mbd.overrideFrom(bd);
}
if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
//如果包含bean存在,且包含Bean不是单例的(也就是意味着是原型的),而当前bean却是单例的,那么当前bean的scope应与包含bean的一致。
mbd.setScope(containingBd.getScope());
}
if (containingBd == null && isCacheBeanMetadata() && isBeanEligibleForMetadataCaching(beanName)) {
this.mergedBeanDefinitions.put(beanName, mbd);//置入缓存
}
}
return mbd;
}
}
3.1.1.2 getObjectFromFactoryBean方法分析
//主要是从FactoryBean获得object
protected Object getObjectFromFactoryBean(FactoryBean factory, String beanName, boolean shouldPostProcess) {
if (factory.isSingleton() && containsSingleton(beanName)) {
//如果facoryBean是单例的话,需要维护factoryBean与object的关系。我们在这里整理一下几个缓存的关系
//<1>factoryBeanObjectCache里存在的一定是由FactoryBean所创建的bean,其对应关系为beanName-->bean 实例;
synchronized (getSingletonMutex()) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
//这里通过调用factory.getObject()获取对象
object = doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));
}
return (object != NULL_OBJECT ? object : null);
}
}
else {
return doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess);
}
}
3.1.2 getSingleton方法分析
public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
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!)");
}
//把beanName加入到正在创建状态队列中,用以标识beanName正在创建中
beforeSingletonCreation(beanName);
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet();
}
try {
//这里将要调用creatBean方法
singletonObject = singletonFactory.getObject();
}
catch (BeanCreationException ex) {
if (recordSuppressedExceptions) {
for (Iterator it = this.suppressedExceptions.iterator(); it.hasNext();) {
ex.addRelatedCause((Exception) it.next());
}
}
throw ex;
}
finally {
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
//把beanName从正在创建队列中删除
afterSingletonCreation(beanName); }
addSingleton(beanName, singletonObject);
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
}
3.1.3 createBean方法分析
protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
throws BeanCreationException {
AccessControlContext acc = AccessController.getContext();
return AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
//检查BeanDefinition中的beanClass是否存在,如果不存在则通过工具类对className处理获得
resolveBeanClass(mbd, beanName);
// Prepare method overrides.
//预处理方法的overload情况(对应lookup-method、replaced-method元素),如果方法在当前beanClass上实现的数量、在beanClass实现的接口声明的数量、在beanClass超类上声明或实现的数量相加的和等于1,那么将overload设为false。如果对lookup-method不熟悉请参看后面的lookup-method使用示例。
//
try {
mbd.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
……
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
//如果bean配置了PostProcessor,那么这里返回的是一个proxy
Object bean = resolveBeforeInstantiation(beanName, mbd);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
//所有生成Bean都有BeanWrapper封装,bean的生成采用策略模式,CglibSubclassingInstantiationStrategy实现是默认的策略
//1.如果配置为工厂方法创建(配置了factory-mother的),由java反射Mehthod的invoke完成bean的创建。
//2.如果采用构造子配置创建(配置了constructor-arg的),如果bean对应的类包含多个构造子,采用cglib动态字节码构造;如果只有唯一的构造子,那么采用java反射Constructor的newInstance方法
//3.如果是普通bean配置,直接通过反射Class默认的Constructor,然后调用newInstance获得bean.
Object beanInstance = doCreateBean(beanName, mbd, args);
if (logger.isDebugEnabled()) {
logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
}, acc);
}
3.1.3.1 doCreateBean方法分析
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {//如果是singleton类型的,先删除factoryBean
instanceWrapper = (BeanWrapper) this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
//所有生成Bean都有BeanWrapper封装,bean的生成采用策略模式,CglibSubclassingInstantiationStrategy实现是默认的策略
//1.如果配置为工厂方法创建(配置了factory-mother的),由java反射Mehthod的invoke完成bean的创建。
//2.如果采用构造子配置创建(配置了constructor-arg的),如果bean对应的类包含多个构造子,采用cglib动态字节码构造;如果只有唯一的构造子,那么采用java反射Constructor的newInstance方法
//3.如果是普通bean配置,直接通过反射Class默认的Constructor,然后调用newInstance获得bean.
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.
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
mbd.postProcessed = true;
}
}
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() {
public Object getObject() throws BeansException {
return getEarlyBeanReference(beanName, mbd, bean);
}
});
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);
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);
}
}
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 actualDependentBeans = new LinkedHashSet(dependentBeans.length);
for (int i = 0; i < dependentBeans.length; i++) {
String dependentBean = dependentBeans[i];
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.
registerDisposableBeanIfNecessary(beanName, bean, mbd);
return exposedObject;
}
posted on 2012-01-30 16:04
zhangxl 阅读(3434)
评论(0) 编辑 收藏 所属分类:
Spring