【Spring源码】依赖注入结合SpringDemo-xml配置理解
【Spring源码】依赖注入结合SpringDemo-xml配置理解
在IOC容器初始化的梳理之后,对依赖注入做一个总结,就是bean实例化的过程,bean的定义有两种方式,一种是xml文件配置,一种是注解,这里是对xml配置文件的依赖注入的介绍,后续对bean与该部分的不同会再做介绍。
一、Demo部分的修改:
首先是xml配置相关,首先在cn.lx.controller包中新建一个TestController2.java,将之前配置的注解的形式全部注释掉,在xml中将bean配置清楚,TestController2的类内容如下:
//@Controller
public class TestController2 {
// @RequestMapping("/test.form")
public void execute(){
return ;
}
}
修改applicationContext.xml文件,为如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test.form">startControl</prop> </props> </property> </bean> <bean id="startControl" class="cn.lx.controller.TestController2"></bean> </beans>
二、依赖注入过程介绍:
依赖注入会发生两种场景,一种是:IOC容器初始化到最后阶段,会将none-Lazy-init的bean全部都实例化,其他的会在具体用到的时候实例化;先从第一种情况IOC容器初始化最后阶段来看实例化的整个过程,实例化的过程后续阶段都是相同的,只是入口不同而已
现在,正文开始,共分为三个步骤:入口-xml配置方式的实例化入口、获取bean、new-bean(实例化bean)
入口部分-xml配置方式的实例化入口:
从AbstractApplicationContext类的refresh()方法中,之前在IOC初始化中主要从obtainFreshBeanFactory()方法调用后得到了ConfigurableListableBeanFactory类型的beanFactory,在beanFactry中已经对xml文件做了解析并且将需要的信息都存储到了beanDefinitionMap中,之后将beanFactory作为参数传递给finishBeanFactoryInitialization方法,进入到其具体实现中:
//完成beanfactory的初始化,初始化所有保留的单例bean protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. //类型转换服务,用于提供线程安全的类型转换 if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Register a default embedded value resolver if no bean post-processor // (such as a PropertyPlaceholderConfigurer bean) registered any before: // at this point, primarily for resolution in annotation attribute values. if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); } // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. //预实例化,对未设置lazy-init=true的进行预实例化处理 beanFactory.preInstantiateSingletons(); }
之后,之后看最后一步,通过beanFactory.preInstantiateSingletons()进入到:DefaultListableBeanFactory类中,该方法会进行判断,会判断是否不是抽象且为singleton且是non lazy-init的,就会进入到是否为FactoryBean的判断,最终发现不是FactoryBean,就进入了else的逻辑分支,即:getBean(beanName)了,从这个部分就进入到下一个关键阶段:获取Bean阶段
@Override public void preInstantiateSingletons() throws BeansException { if (logger.isTraceEnabled()) { logger.trace("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<>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans... //触发对于所有非懒加载的singleton beans的实例化 //通过getBean()方法重点执行,其中getBean会找到doGetBean for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); //判断是否不为abstract、为singleton、lazyinit属性为false,则进入到下一步 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { //判断是否是FactoryBean if (isFactoryBean(beanName)) { Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { final FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } } //判断不是FactoryBean,直接进入getBean逻辑 else { getBean(beanName); } } }
获取Bean阶段:
从亮色的:getBean方法进入到类:AbstractBeanFactory中,然后就看到do真正做事情的来啦:
@Override public Object getBean(String name) throws BeansException { //实际执行 return doGetBean(name, null, null, false); }
兴奋地点击doGetBean,去看看它究竟干了啥?进入到类:AbstractBeanFactory类中,doGetBean所完成的内容主要包含:判断是否已经有存在singleton的beanName,若有可以直接返回,若无则获取此bean所依赖的beans-dependsOn,然后通过一个for循环判断,是否这个中的所有dependsOn中的某个bean是否存在环形依赖,若存在的话,直接抛出异常,无异常则为给定的bean注册依赖bean(依赖bean也会在给定bean销毁之前被销毁),最后走到createBean逻辑,至此进入到下一个阶段:new-bean真正创建的过程了
doGetBean部分的重要代码如下:
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { //规范化beanName final String beanName = transformedBeanName(name); //类型:Object Object bean; // Eagerly check singleton cache for manually registered singletons. //获取已经创建的singleton的bean的实例对象,将之赋给Object的sharedInstance Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { if (logger.isTraceEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.trace("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.trace("Returning cached instance of singleton bean '" + beanName + "'"); } } //对于已经存在的直接返回 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } //没有已经存在的singleton的bean的实例对象 else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null) { // Delegation to parent with explicit args. return (T) parentBeanFactory.getBean(nameToLookup, args); } else if (requiredType != null) { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } else { return (T) parentBeanFactory.getBean(nameToLookup); } } if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. //确保被依赖的bean要初始化之后,其他的才能继续初始化 String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dep : dependsOn) { if (isDependent(beanName, dep)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } //给给定bean注册依赖bean registerDependentBean(dep, beanName); try { getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", ex); } } } // Create bean instance. //实例化 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, ()-> { try { //实例化 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 = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { // It's a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, () -> { 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); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // Check if required type matches the type of the actual bean instance. if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isTraceEnabled()) { logger.trace("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; }
createBean过程:
进入到AbstractAutowireCapableBeanFactory类的createBean方法中,该方法主要做了三件事情:首先将RootBeanDefinition(注:一个RootBeanDefinition类型的mbdToUse,RootBeanDefinition就是根bean定义,表示合并的bean定义,在运行时支持Spring Factory中定义的特定的bean)类的一个实例对其设置beanClass,并验证和准备method override,其次在真正进行create之前先判断是会否有short-cut快捷方式可以直接返回,最后,进入到doCreateBean()中,其中doCreateBean()方法的参数有三个,一个是:一个是beanName,一个是mbdToUse,一个是参数args;关键部分的代码如下:
//依赖注入,实例化一个bean @Override protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { if (logger.isTraceEnabled()) { logger.trace("Creating instance of bean '" + beanName + "'"); } //一个RootBeanDefinition类型的mbdToUse //RootBeanDefinition就是根bean定义,表示合并的bean定义,在运行时支持Spring Factory中定义的特定的bean RootBeanDefinition mbdToUse = mbd; // Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class // which cannot be stored in the shared merged bean definition. //resolveBeanClass的作用是:根据特定的beanDefinition解析bean class,并得到一个class的引用;将已解析的类存储到bean定义中以供后续使用 Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { //将解析后得到的resolvedCalss设置给RootBeanDefinition 根bean定义的 mbdToUse mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } //在RootBeanDefinition中可以去准备Bean所需要的各种属性,例如:beanClass解析后的类信息的set,例如method override //这些信息在后续去进行实际的创建bean实例化的过程都是需要用到的 // Prepare method overrides. try { //要回头去看解析bean的存入到BeanDefinition中的时候,就已经设置过overrides的相关属性了 mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. //在实例化之前判断,是否有short-cut快捷方式可以应用 Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } try { //doCreateBean是真正做实例化操作的部分 //这里的三个参数,一个是beanName,一个是mbdToUse,一个是参数args Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isTraceEnabled()) { logger.trace("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { // A previously detected exception with proper bean creation context already, // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry. throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex); } }
继续:doCreateBean,会做三件事情:首先,判断是否是单例,如果单例的话,需要将其FactoryBeanInstanceCache中的该beanName取出,若为null,则需要继续创建,否则直接用cache中取出的接口;后面会重点说一下:创建createBeanInstance的过程,之后initializeBean,最后registerDisposableBeanIfNecessary,将bean注册为一次性的
主要部分代码代码见下方:
//方法说明:真正执行CreatBean的操作 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. BeanWrapper instanceWrapper = null; //判断是单例,则从factoryBeanInstanceCache中将其取出,若存在,则基于当前已经缓存的进行后续处理 //否则需要继续创建 if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //创建一个特定bean的new instance新的实例 instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Initialize the bean instance. //填充过程 Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); //initializeBean 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); } } // Register bean as disposable. //将bean注册为一次性的 try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
下面重点说明:createBeanInstance过程,会根据有参和无参,分别进入到:autowireConstructor和instantiateBean中,重点代码见:
//创建一个特定bean的new instance新的实例 //会使用恰当的实例化策略:工厂方法、构造函数自动装配或简单实例化 protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { // Make sure bean class is actually resolved at this point. Class<?> beanClass = resolveBeanClass(mbd, beanName); 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()); } Supplier<?> instanceSupplier = mbd.getInstanceSupplier(); if (instanceSupplier != null) { return obtainFromSupplier(instanceSupplier, beanName); } if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // Shortcut when re-creating the same 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); } } // Candidate constructors for autowiring? Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // Preferred constructors for default construction? ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // No special handling: simply use no-arg constructor. //调用无参构造器 return instantiateBean(beanName, mbd); }
如果是有参所用到的autowireConstructor,主要是:解析参数、获取constructorToUse,如果一直没有找到可用的,直接抛出异常,有可用的继续bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));进入instantiate中,该方法是在:ContructorReSolver类中,该方法中可以看到:strategy的字眼,这里针对不同的情况进行了不同的策略选择,具体见下方:
/类内部的私有方法 //实例化,通过对应的Constructor和参数进行某个类的对象的实例化 //autowire的走到了这里 private Object instantiate( String beanName, RootBeanDefinition mbd, Constructor constructorToUse, Object[] argsToUse) { try { //获取初始化策略 InstantiationStrategy strategy = this.beanFactory.getInstantiationStrategy(); if (System.getSecurityManager() != null) { return AccessController.doPrivileged((PrivilegedAction<Object>) () -> strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse), this.beanFactory.getAccessControlContext()); } else { return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse); } } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean instantiation via constructor failed", ex); } }
那么具体所给出的不同策略的支持是什么呢?进入到了:对于jdk动态代理和cglib方式的选择了
SimpleInstantiationStrategy类中,
//最终的很多实现的调到的都是:Simplexxx、或者是Defalutxxx之类的 //这是有参数的contructor的走的路径 @Override public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, final Constructor<?> ctor, Object... args) { //没有method是override的情况,就是说不能通过继承来实现的,那么必须不能用cglib了, //否则可以用cglib动态代理 if (!bd.hasMethodOverrides()) { if (System.getSecurityManager() != null) { // use own privileged to change accessibility (when security is on) AccessController.doPrivileged((PrivilegedAction<Object>) () -> { ReflectionUtils.makeAccessible(ctor); return null; }); } return BeanUtils.instantiateClass(ctor, args); } else { return instantiateWithMethodInjection(bd, beanName, owner, ctor, args); } }
到这里我们对整个过程做一个总结:从入口进入之后,因为是注入依赖实例化bean的过程,所以在这个过程中,会不断判断是否有可用的快捷方式,是什么类型的,是singleton的,还是protype类型的,进行getBean获取bean的操作时,针对已经创建的可以直接返回的直接结束,若无则进行创建,创建中会要判断是否有parentFactory,是否有依赖bean,set创建过程中可能需要的条件,例如method overrides的验证和准备等,获取可用的constructor,有可用的构造器,有参数的情况等,都要走自主选择的逻辑,否则全部最后走兜底策略,就是默认构造器,注意,在对constructor选择的过程中,可能会出现无可用,或者是Ambiguous的情况,会抛出异常;具体最终创建bean要用什么策略,提供两种选择
三、依赖注入过程时序图:(备注:原图若出现看不清的情况,可下载后查看,请保留版权)