Spring IOC源代码具体解释之容器依赖注入
加载bean定义文件的过程。这个过程是通过BeanDefinitionReader来完毕的。当中通过 loadBeanDefinition()来对定义文件进行解析和依据Spring定义的bean规则进行处理 - 其实和Spring定义的bean规则相关的处理是在BeanDefinitionParserDelegate中完毕的,完毕这个处理须要得到用户定义的bean定义信息,这是在前面已经通过解析已经完毕了的。这个处理过程的结果就是得到了一系列的BeanDefinition,这里不但包括了对 bean定义信息解析后的表示。同一时候还把和这个bean相关的依赖信息也保存了下来。把这些BeanDefinition都放到一个HashMap - beanDefinitionMap里面
基本知识
BeanFactory接口定义了Spring IoC容器的基本功能规范,是Spring IoC容器所应遵守的最底层和最主要的编程规范。
BeanFactory接口中定义了几个getBean方法,就是用户向IoC容器索取管理的Bean的方法。
选择AbstractBeanFactory,作为子类,找到getbean方法
@Override public Object getBean(String name) throws BeansException { return doGetBean(name, null, null, false); } @Override publicT getBean(String name, Class requiredType) throws BeansException { return doGetBean(name, requiredType, null, false); } @Override public Object getBean(String name, Object... args) throws BeansException { return doGetBean(name, null, args, false); } public T getBean(String name, Class requiredType, Object... args) throws BeansException { return doGetBean(name, requiredType, args, false); }
继续进入
//真正实现向IoC容器获取Bean 的 功 能。也是触发依赖注入功能的地方 @SuppressWarnings("unchecked") protectedT doGetBean( final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { //依据指定的名称获取被管理Bean的名称。剥离指定名称中对容器的相关依赖 //假设指定的是别名。将别名转换为规范的Bean名称 final String beanName = transformedBeanName(name); Object bean; //先从缓存中取是否已经有被创建过的单态类型的Bean。对于单态模式的Bean整 //个IoC容器中仅仅创建一次。不须要反复创建 Object sharedInstance = getSingleton(beanName); //IoC容器创建单态模式Bean实例对象 if (sharedInstance != null && args == null) { if (logger.isDebugEnabled()) { //假设指定名称的Bean在容器中已有单态模式的Bean被创建,直接返回 //已经创建的Bean if (isSingletonCurrentlyInCreation(beanName)) { logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } } //获取给定Bean的实例对象,主要是完毕FactoryBean的相关处理 //注意:BeanFactory是管理容器中Bean的工厂,而FactoryBean是 //创建创建对象的工厂Bean。两者之间有差别 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else {//缓存没有正在创建的单态模式Bean //缓存中已经有已经创建的原型模式Bean。可是因为循环引用的问题导致实 //例化对象失败 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } //对IoC容器中是否存在指定名称的BeanDefinition进行检查,首先检查是否 //能在当前的BeanFactory中获取的所须要的Bean。假设不能则托付当前容器 //的父级容器去查找,假设还是找不到则沿着容器的继承体系向父级容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //当前容器的父级容器存在,且当前容器中不存在指定名称的Bean if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { //解析指定Bean名称的原始名称 String nameToLookup = originalBeanName(name); if (args != null) { //委派父级容器依据指定名称和显式的參数查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { //委派父级容器依据指定名称和类型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } } //创建的Bean是否须要进行类型验证。一般不须要 if (!typeCheckOnly) { //向容器标记指定的Bean已经被创建 markBeanAsCreated(beanName); } //依据指定Bean名称获取其父级的Bean定义。主要解决Bean继承时子类 //合并父类公共属性问题 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); //获取当前Bean全部依赖Bean的名称 String[] dependsOn = mbd.getDependsOn(); //假设当前Bean有依赖Bean if (dependsOn != null) { for (String dependsOnBean : dependsOn) { //递归调用getBean方法,获取当前Bean的依赖Bean getBean(dependsOnBean); //把被依赖Bean注冊给当前依赖的Bean registerDependentBean(dependsOnBean, beanName); } } //创建单态模式Bean的实例对象 if (mbd.isSingleton()) { //这里使用了一个匿名内部类,创建Bean实例对象。而且注冊给所依赖的对象 sharedInstance = getSingleton(beanName, new ObjectFactory() { public Object getObject() throws BeansException { try { //创建一个指定Bean实例对象。假设有父级继承。则合并子//类和父类的定义 return createBean(beanName, mbd, args); } catch (BeansException ex) { //显式地从容器单态模式Bean缓存中清除实例对象 destroySingleton(beanName); throw ex; } } }); //获取给定Bean的实例对象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //IoC容器创建原型模式Bean实例对象 else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都会创建一个新的对象 Object prototypeInstance = null; try { //回调beforePrototypeCreation方法。默认的功能是注冊当前创//建的原型对象 beforePrototypeCreation(beanName); //创建指定Bean对象实例 prototypeInstance = createBean(beanName, mbd, args); } finally { //回调afterPrototypeCreation方法,默认的功能告诉IoC容器指//定Bean的原型对象不再创建了 afterPrototypeCreation(beanName); } //获取给定Bean的实例对象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要创建的Bean既不是单态模式。也不是原型模式,则依据Bean定义资源中 //配置的生命周期范围,选择实例化Bean的合适方法。这样的在Web应用程序中 //比較经常使用。如:request、session、application等生命周期 else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); //Bean定义资源中没有配置生命周期范围,则Bean定义不合法 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的实例对象 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); } } } //对创建的Bean实例对象进行类型检查 if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return (T) bean; }
上面,我们能够清楚的看见在创建实例是做了推断
-
假设Bean定义的单态模式(Singleton),则容器在创建之前先从缓存中查找。以确保整个容器中仅仅存在一个实例对象
-
假设Bean定义的是原型模式(Prototype),则容器每次都会创建一个新的实例对象。
-
两者都不是,则依据Bean定义资源中配置的生命周期范围,选择实例化Bean的合适方法,这样的在Web应用程序中 比較经常使用。如:request、session、application等生命周期
上面的源代码仅仅是定义了依据Bean定义的模式,採取的不同创建Bean实例对象的策略。详细的Bean实例对象的创建过程由实现了ObejctFactory接口的匿名内部类的createBean方法完毕。ObejctFactory使用委派模式。详细的Bean实例创建过程交由事实上现类AbstractAutowireCapableBeanFactory完毕,我们继续分析AbstractAutowireCapableBeanFactory的createBean方法的源代码,理解其创建Bean实例的详细实现过程
匿名内部类
sharedInstance = getSingleton(beanName, new ObjectFactory
AbstractAutowireCapableBeanFactory的createBean,这里之所以进入AbstractAutowireCapableBeanFactory,是由于AbstractAutowireCapableBeanFactory继承了AbstractBeanFactory
//创建Bean实 例对象 protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } //推断须要创建的Bean能否够实例化,即能否够通过当前的类载入器载入 resolveBeanClass(mbd, beanName); //校验和准备Bean中的方法覆盖 try { mbd.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { //假设Bean配置了初始化前和初始化后的处理器,则试图返回一个须要创建//Bean的代理对象 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的入口 Object beanInstance = doCreateBean(beanName, mbd, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } //真正创建Bea n 的方法 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { //封装被创建的Bean对象 BeanWrapper instanceWrapper = null; if (mbd.isSingleton()){//单态模式的Bean,先从容器中缓存中获取同名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); //调用PostProcessor后置处理器 synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); mbd.postProcessed = true; } } // Eagerly cache singletons to be able to resolve circular references //向容器中缓存单态模式的Bean对象,以防循环引用 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); } }); } //Bean对象的初始化,依赖注入在此触发 //这个exposedObject在初始化完毕之后返回作为依赖注入完毕后的Bean Object exposedObject = bean; try { //将Bean实例对象封装,而且Bean定义中配置的属性值赋值给实例对象 populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { //初始化Bean对象 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) { //获取指定名称的已注冊的单态模式Bean对象 Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { //依据名称获取的以注冊的Bean和正在实例化的Bean是同一个 if (exposedObject == bean) { //当前实例化的Bean初始化完毕 exposedObject = earlySingletonReference; } //当前Bean依赖其它Bean,而且当发生循环引用时不同意新创建实例对象 else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); SetactualDependentBeans = new LinkedHashSet (dependentBeans.length); //获取当前Bean所依赖的其它Bean for (String dependentBean : dependentBeans) { //对依赖Bean进行类型检查 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."); } } } } //注冊完毕依赖注入的Bean try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
主要实如今下面两个方法中:
-
createBeanInstance:生成Bean所包括的java对象实例。
-
populateBean :对Bean属性的依赖注入进行处理。
createBeanInstance:生成Bean所包括的java对象实例。
在Docreate中找到这句:
instanceWrapper = createBeanInstance(beanName, mbd, args);
//创建Bean的实例对象 protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) { //检查确认Bean是可实例化的 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); } //使用容器的自己主动装配方法进行实例化 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) { //配置了自己主动装配属性,使用容器的自己主动装配实例化 //容器的自己主动装配是依据參数类型匹配Bean的构造方法 return autowireConstructor(beanName, mbd, null, null); } else { //使用默认的无參构造方法实例化 return instantiateBean(beanName, mbd); } } //使用Bean的构造方法进行实例化 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); }
继续进入instantiateBean
//使用默认的无參构造方法实例化Bean对象 protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { try { Object beanInstance; final BeanFactory parent = this; //获取系统的安全管理接口,JDK标准的安全管理API if (System.getSecurityManager() != null) { //这里是一个匿名内置类。依据实例化策略创建实例对象 beanInstance = AccessController.doPrivileged(new PrivilegedAction
:org.springframework.beans.BeanWrapper是Spring框架中重要的组件类。BeanWrapper相当于一个代理器,Spring通过BeanWrapper完毕Bean属性的填充工作。在Bean实例被InstantiationStrategy创建出来之后。容器主控程序将Bean实例通过BeanWrapper包装起来。
所以BeanWrapper实现类BeanWrapperImpl具有了三重身份:
- 1)Bean包裹器。
- 2)属性訪问器;
- 3)属性编辑器注冊表。
在注入过程中:
createBean開始调用populateBean方法.首先进行自己主动装配模式的处理,也就是说对BeanDefiniton中定义自己主动装配模式的属性进行调整.比方定义了AUTOWIRE_BY_NAME的属性会对应找到匹配该命名的bean.而且将该被检索到的bean实例值给property,当然这里也肯定通过getBean方法来获取这个须要自己主动装配的bean实例,而且在BeanWrapper中包装
populateBean :对Bean属性的依赖注入进行处理
-
属性值解析
-
注入
//将Bean属性设置到生成的实例对象上 protected void populateBean(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw) { //获取容器在解析Bean定义资源时为BeanDefiniton中设置的属性值 PropertyValues pvs = mbd.getPropertyValues(); //实例对象为null if (bw == null) { //属性值不为空 if (!pvs.isEmpty()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance"); } else { //实例对象为null,属性值也为空,不须要设置属性值,直接返回 return; } } //在设置属性之前调用Bean的PostProcessor后置处理器 boolean continueWithPropertyPopulation = true; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } //依赖注入開始。首先处理autowire自己主动装配的注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); //对autowire自己主动装配的处理。依据Bean名称自己主动装配注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } //依据Bean类型自己主动装配注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } //检查容器是否持实用于处理单态模式Bean关闭时的后置处理器 boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); //Bean实例对象没有依赖,即没有继承基类 boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { //从实例对象中提取属性描写叙述符 PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw); if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //使用BeanPostProcessor处理器处理属性值 pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } if (needsDepCheck) { //为要设置的属性进行依赖检查 checkDependencies(beanName, mbd, filteredPds, pvs); } } //对属性进行注入 applyPropertyValues(beanName, mbd, bw, pvs); }
继续applyPropertyValues
//解析并注入依赖属性的过程 protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) { if (pvs == null || pvs.isEmpty()) { return; } //封装属性值 MutablePropertyValues mpvs = null; Listoriginal; if (System.getSecurityManager()!= null) { if (bw instanceof BeanWrapperImpl) { //设置安全上下文。JDK安全机制 ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext()); } } if (pvs instanceof MutablePropertyValues) { mpvs = (MutablePropertyValues) pvs; //属性值已经转换 if (mpvs.isConverted()) { try { //为实例化对象设置属性值 bw.setPropertyValues(mpvs); return; } catch (BeansException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Error setting property values", ex); } } //获取属性值对象的原始类型值 original = mpvs.getPropertyValueList(); } else { original = Arrays.asList(pvs.getPropertyValues()); } //获取用户自己定义的类型转换 TypeConverter converter = getCustomTypeConverter(); if (converter == null) { converter = bw; } //创建一个Bean定义属性值解析器,将Bean定义中的属性值解析为Bean实例对象 //的实际值 BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter); //为属性的解析值创建一个拷贝,将拷贝的数据注入到实例对象中 List deepCopy = new ArrayList (original.size()); boolean resolveNecessary = false; for (PropertyValue pv : original) { //属性值不须要转换 if (pv.isConverted()) { deepCopy.add(pv); } //属性值须要转换 else { String propertyName = pv.getName(); //原始的属性值,即转换之前的属性值 Object originalValue = pv.getValue(); //转换属性值,比如将引用转换为IoC容器中实例化对象引用 Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); //转换之后的属性值 Object convertedValue = resolvedValue; //属性值能否够转换 boolean convertible = bw.isWritableProperty(propertyName) && !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName); if (convertible) { //使用用户自己定义的类型转换器转换属性值 convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter); } //存储转换后的属性值,避免每次属性注入时的转换工作 if (resolvedValue == originalValue) { if (convertible) { //设置属性转换之后的值 pv.setConvertedValue(convertedValue); } deepCopy.add(pv); } //属性是可转换的,且属性原始值是字符串类型,且属性的原始类型值不是 //动态生成的字符串。且属性的原始值不是集合或者数组类型 else if (convertible && originalValue instanceof TypedStringValue && !((TypedStringValue) originalValue).isDynamic() && !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) { pv.setConvertedValue(convertedValue); deepCopy.add(pv); } else { resolveNecessary = true; //又一次封装属性的值 deepCopy.add(new PropertyValue(pv, convertedValue)); } } } if (mpvs != null && !resolveNecessary) { //标记属性值已经转换过 mpvs.setConverted(); } //进行属性依赖注入 try { bw.setPropertyValues(new MutablePropertyValues(deepCopy)); } catch (BeansException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Error setting property values", ex); } }
从上面代码能够看出属性转换分为了两种情况
-
属性值类型不须要转换时,不须要解析属性值,直接准备进行依赖注入。
-
属性值须要进行类型转换时,如对其它对象的引用等,首先须要解析属性值,然后对解析后的属性值进行依赖注入。
对属性值的解析是在BeanDefinitionValueResolver类中的resolveValueIfNecessary方法中进行的,对属性值的依赖注入是通过bw.setPropertyValues方法实现的
resolveValueIfNecessary对属性值的解析
当容器在对属性进行依赖注入时,假设发现属性值须要进行类型转换,如属性值是容器中还有一个Bean实例对象的引用,则容器首先须要依据属性值解析出所引用的对象。然后才干将该引用对象注入到目标实例对象的属性上去,对属性进行解析的由resolveValueIfNecessary方法实现。
可知:创建与注入是个递归的过程
//解析属性值。对注入类型进行转换 public Object resolveValueIfNecessary(Object argName, Object value) { //对引用类型的属性进行解析 if (value instanceof RuntimeBeanReference) { RuntimeBeanReference ref = (RuntimeBeanReference) value; //调用引用类型属性的解析方法 return resolveReference(argName, ref); } //对属性值是引用容器中还有一个Bean名称的解析 else if (value instanceof RuntimeBeanNameReference) { String refName = ((RuntimeBeanNameReference) value).getBeanName(); refName = String.valueOf(evaluate(refName)); //从容器中获取指定名称的Bean if (!this.beanFactory.containsBean(refName)) { throw new BeanDefinitionStoreException( "Invalid bean name '" + refName + "' in bean reference for " + argName); } return refName; } //对Bean类型属性的解析,主要是Bean中的内部类 else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value; return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition()); } else if (value instanceof BeanDefinition) { BeanDefinition bd = (BeanDefinition) value; return resolveInnerBean(argName, "(inner bean)", bd); } //对集合数组类型的属性解析 else if (value instanceof ManagedArray) { ManagedArray array = (ManagedArray) value; //获取数组的类型 Class elementType = array.resolvedElementType; if (elementType == null) { //获取数组元素的类型 String elementTypeName = array.getElementTypeName(); if (StringUtils.hasText(elementTypeName)) { try { //使用反射机制创建指定类型的对象 elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader()); array.resolvedElementType = elementType; } catch (Throwable ex) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error resolving array type for " + argName, ex); } } //没有获取到数组的类型,也没有获取到数组元素的类型,则直接设置数 //组的类型为Object else { elementType = Object.class; } } //创建指定类型的数组 return resolveManagedArray(argName, (List ) value, elementType); } //解析list类型的属性值 else if (value instanceof ManagedList) { return resolveManagedList(argName, (List ) value); } //解析set类型的属性值 else if (value instanceof ManagedSet) { return resolveManagedSet(argName, (Set ) value); } //解析map类型的属性值 else if (value instanceof ManagedMap) { return resolveManagedMap(argName, (Map
>) value); } //解析props类型的属性值。props事实上就是key和value均为字符串的map else if (value instanceof ManagedProperties) { Properties original = (Properties) value; //创建一个拷贝,用于作为解析后的返回值 Properties copy = new Properties(); for (Map.Entry propEntry : original.entrySet()) { Object propKey = propEntry.getKey(); Object propValue = propEntry.getValue(); if (propKey instanceof TypedStringValue) { propKey = evaluate((TypedStringValue) propKey); } if (propValue instanceof TypedStringValue) { propValue = evaluate((TypedStringValue) propValue); } copy.put(propKey, propValue); } return copy; } //解析字符串类型的属性值 else if (value instanceof TypedStringValue) { TypedStringValue typedStringValue = (TypedStringValue) value; Object valueObject = evaluate(typedStringValue); try { //获取属性的目标类型 Class<?
> resolvedTargetType = resolveTargetType(typedStringValue); if (resolvedTargetType != null) { //对目标类型的属性进行解析,递归调用 return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType); } //没有获取到属性的目标对象。则按Object类型返回 else { return valueObject; } } catch (Throwable ex) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error converting typed String value for " + argName, ex); } } else { return evaluate(value); } }
假设是引用类型。则进入resolveReference
//解析引用类型的属性值 private Object resolveReference(Object argName, RuntimeBeanReference ref) { try { //获取引用的Bean名称 String refName = ref.getBeanName(); refName = String.valueOf(evaluate(refName)); //假设引用的对象在父类容器中,则从父类容器中获取指定的引用对象 if (ref.isToParent()) { if (this.beanFactory.getParentBeanFactory() == null) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Can't resolve reference to bean '" + refName + "' in parent factory: no parent factory available"); } return this.beanFactory.getParentBeanFactory().getBean(refName); } //从当前的容器中获取指定的引用Bean对象。假设指定的Bean没有被实例化 //则会递归触发引用Bean的初始化和依赖注入 else { Object bean = this.beanFactory.getBean(refName); //将当前实例化对象的依赖引用对象 this.beanFactory.registerDependentBean(refName, this.beanName); return bean; } } catch (BeansException ex) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex); } }
setPropertyValues方法实现对属性值的依赖注入
BeanWrapperImpl类主要是对容器中完毕初始化的Bean实例对象进行属性的依赖注入。即把Bean对象设置到它所依赖的还有一个Bean的属性中去
public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements BeanWrapper
BeanWrapperImpl继承了AbstractNestablePropertyAccessor。相应的setPropertyValues方法也在AbstractNestablePropertyAccessor中,进入AbstractNestablePropertyAccessor
里面有三个setPropertyValues方法,前两个都指向了最后一个:
//实现属性依赖注入功能 private void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException { //PropertyTokenHolder主要保存属性的名称、路径,以及集合的size等信息 String propertyName = tokens.canonicalName; String actualName = tokens.actualName; //keys是用来保存集合类型属性的size if (tokens.keys != null) { //将属性信息拷贝 PropertyTokenHolder getterTokens = new PropertyTokenHolder(); getterTokens.canonicalName = tokens.canonicalName; getterTokens.actualName = tokens.actualName; getterTokens.keys = new String[tokens.keys.length - 1]; System.arraycopy(tokens.keys, 0, getterTokens.keys, 0, tokens.keys.length - 1); Object propValue; try { //获取属性值,该方法内部使用JDK的内省( Introspector)机制,调用属性//的getter(readerMethod)方法。获取属性的值 propValue = getPropertyValue(getterTokens); } catch (NotReadablePropertyException ex) { throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName, "Cannot access indexed value in property referenced " + "in indexed property path '" + propertyName + "'", ex); } //获取集合类型属性的长度 String key = tokens.keys[tokens.keys.length - 1]; if (propValue == null) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName, "Cannot access indexed value in property referenced " + "in indexed property path '" + propertyName + "': returned null"); } //注入array类型的属性值 else if (propValue.getClass().isArray()) { //获取属性的描写叙述符 PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //获取数组的类型 Class requiredType = propValue.getClass().getComponentType(); //获取数组的长度 int arrayIndex = Integer.parseInt(key); Object oldValue = null; try { //获取数组曾经初始化的值 if (isExtractOldValueForEditor()) { oldValue = Array.get(propValue, arrayIndex); } //将属性的值赋值给数组中的元素 Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), requiredType)); Array.set(propValue, arrayIndex, convertedValue); } catch (IndexOutOfBoundsException ex) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Invalid array index in property path '" + propertyName + "'", ex); } } //注入list类型的属性值 else if (propValue instanceof List) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //获取list集合的类型 Class requiredType = GenericCollectionTypeResolver.getCollectionReturnType( pd.getReadMethod(), tokens.keys.length); List list = (List) propValue; //获取list集合的size int index = Integer.parseInt(key); Object oldValue = null; if (isExtractOldValueForEditor() && index < list.size()) { oldValue = list.get(index); } //获取list解析后的属性值 Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), requiredType)); if (index < list.size()) { //为list属性赋值 list.set(index, convertedValue); } //假设list的长度大于属性值的长度,则多余的元素赋值为null else if (index >= list.size()) { for (int i = list.size(); i < index; i++) { try { list.add(null); } catch (NullPointerException ex) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Cannot set element with index " + index + " in List of size " + list.size() + ", accessed using property path '" + propertyName + "': List does not support filling up gaps with null elements"); } } list.add(convertedValue); } } //注入map类型的属性值 else if (propValue instanceof Map) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //获取map集合key的类型 Class mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType( pd.getReadMethod(), tokens.keys.length); //获取map集合value的类型 Class mapValueType = GenericCollectionTypeResolver.getMapValueReturnType( pd.getReadMethod(), tokens.keys.length); Map map = (Map) propValue; //解析map类型属性key值 Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), mapKeyType)); Object oldValue = null; if (isExtractOldValueForEditor()) { oldValue = map.get(convertedMapKey); } //解析map类型属性value值 Object convertedMapValue = convertIfNecessary( propertyName, oldValue, pv.getValue(), mapValueType, new TypeDescriptor(new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length + 1))); //将解析后的key和value值赋值给map集合属性 map.put(convertedMapKey, convertedMapValue); } else { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Property referenced in indexed property path '" + propertyName + "' is neither an array nor a List nor a Map; returned value was [" + pv.getValue() + "]"); } } //对非集合类型的属性注入 else { PropertyDescriptor pd = pv.resolvedDescriptor; if (pd == null || !pd.getWriteMethod().getDeclaringClass().isInstance(this.object)) { pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //无法获取到属性名或者属性没有提供setter(写方法)方法 if (pd == null || pd.getWriteMethod() == null) { //假设属性值是可选的,即不是必须的。则忽略该属性值 if (pv.isOptional()) { logger.debug("Ignoring optional value for property '" + actualName + "' - property not found on bean class [" + getRootClass().getName() + "]"); return; } //假设属性值是必须的,则抛出无法给属性赋值,由于每天提供setter方法异常 else { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), this.nestedPath + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); } } pv.getOriginalPropertyValue().resolvedDescriptor = pd; } Object oldValue = null; try { Object originalValue = pv.getValue(); Object valueToApply = originalValue; if (!Boolean.FALSE.equals(pv.conversionNecessary)) { if (pv.isConverted()) { valueToApply = pv.getConvertedValue(); } else { if (isExtractOldValueForEditor() && pd.getReadMethod() != null) { //获取属性的getter方法(读方法),JDK内省机制 final Method readMethod = pd.getReadMethod(); //假设属性的getter方法不是public訪问控制权限的,即訪问控制权限比較严格。 //则使用JDK的反射机制强行訪问非public的方法(暴力读取属性值) if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()) && !readMethod.isAccessible()) { if (System.getSecurityManager()!= null) { //匿名内部类,依据权限改动属性的读取控制限制 AccessController.doPrivileged(new PrivilegedAction
有上面分析,Spring IoC容器将属性的值注入到Bean实例对象中大致例如以下
-
对于集合类型的属性,将其属性值解析为目标类型的集合后直接赋值给属性
-
对于非集合类型的属性,大量使用了JDK的反射和内省机制,通过属性的getter方法(reader method)获取指定属性注入曾经的值,同一时候调用属性的setter方法(writer method)为属性设置注入后的值。
至此,SpringIOC依赖注入就基本到这里了