跟跟Springboot启动容器,自动装配的过程

-----------------------------------------------------本文只作为跟代码的一个参考,建议可以根据思路在指定类中断点调试学习------------------------------------------------------------

运行被@SpringBootApplication修饰的程序入口,执行main方法,调用SpringApplication的run方法。

下面是从创建controller层bean的方法调用链如下:(备注:controller层做了通过mybaits连接数据库返回查询结果的操作)以下是倒序描述

// 这里就是调用了SqlsessionTemplate的构造方法
<init>:94, SqlSessionTemplate (org.mybatis.spring) sqlSessionTemplate:163, MybatisAutoConfiguration (org.mybatis.spring.boot.autoconfigure) CGLIB$sqlSessionTemplate$1:-1, MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$2abe972c (org.mybatis.spring.boot.autoconfigure) invoke:-1, MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$2abe972c$$FastClassBySpringCGLIB$$d1075e2f (org.mybatis.spring.boot.autoconfigure) invokeSuper:244, MethodProxy (org.springframework.cglib.proxy) intercept:363, ConfigurationClassEnhancer$BeanMethodInterceptor (org.springframework.context.annotation) sqlSessionTemplate:-1, MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$2abe972c (org.mybatis.spring.boot.autoconfigure) invoke0:-1, NativeMethodAccessorImpl (sun.reflect) invoke:62, NativeMethodAccessorImpl (sun.reflect) invoke:43, DelegatingMethodAccessorImpl (sun.reflect) invoke:498, Method (java.lang.reflect) instantiate:154, SimpleInstantiationStrategy (org.springframework.beans.factory.support) instantiate:651, ConstructorResolver (org.springframework.beans.factory.support) instantiateUsingFactoryMethod:636, ConstructorResolver (org.springframework.beans.factory.support) instantiateUsingFactoryMethod:1338, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBeanInstance:1177, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)//这里和其他bean的创建是不一样的,后面的操作也是在这个方法中执行的,执行完这个方法的后续操作和其他bean是一样的 doCreateBean:557, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:323, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 1658980982 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$145) getSingleton:222, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support) doGetBean:321, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support)
//5、这里开始创建SqlSessionTemplate实例 resolveCandidate:276, DependencyDescriptor (org.springframework.beans.factory.config) doResolveDependency:1287, DefaultListableBeanFactory (org.springframework.beans.factory.support) resolveDependency:1207, DefaultListableBeanFactory (org.springframework.beans.factory.support) autowireByType:1511, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) populateBean:1406, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) doCreateBean:594, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:323, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 1658980982 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$145) getSingleton:222, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support) doGetBean:321, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support)
//4、开始创建mapper接口代理实例 resolveCandidate:276, DependencyDescriptor (org.springframework.beans.factory.config) doResolveDependency:1287, DefaultListableBeanFactory (org.springframework.beans.factory.support) resolveDependency:1207, DefaultListableBeanFactory (org.springframework.beans.factory.support) autowireResource:537, CommonAnnotationBeanPostProcessor (org.springframework.context.annotation) getResource:513, CommonAnnotationBeanPostProcessor (org.springframework.context.annotation) getResourceToInject:653, CommonAnnotationBeanPostProcessor$ResourceElement (org.springframework.context.annotation) inject:224, InjectionMetadata$InjectedElement (org.springframework.beans.factory.annotation) inject:116, InjectionMetadata (org.springframework.beans.factory.annotation) postProcessProperties:334, CommonAnnotationBeanPostProcessor (org.springframework.context.annotation) populateBean:1422, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) doCreateBean:594, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:323, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 1658980982 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$145) getSingleton:222, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support) doGetBean:321, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support)
// 3、开始创建manager层bean resolveCandidate:276, DependencyDescriptor (org.springframework.beans.factory.config) doResolveDependency:1287, DefaultListableBeanFactory (org.springframework.beans.factory.support) resolveDependency:1207, DefaultListableBeanFactory (org.springframework.beans.factory.support) autowireResource:537, CommonAnnotationBeanPostProcessor (org.springframework.context.annotation) getResource:513, CommonAnnotationBeanPostProcessor (org.springframework.context.annotation) getResourceToInject:653, CommonAnnotationBeanPostProcessor$ResourceElement (org.springframework.context.annotation) inject:224, InjectionMetadata$InjectedElement (org.springframework.beans.factory.annotation) inject:116, InjectionMetadata (org.springframework.beans.factory.annotation) postProcessProperties:334, CommonAnnotationBeanPostProcessor (org.springframework.context.annotation) populateBean:1422, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) doCreateBean:594, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:323, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 1658980982 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$145) getSingleton:222, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support) // 循环调用创建bean的方法 doGetBean:321, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support)
// 2、开始创建service层bean resolveCandidate:276, DependencyDescriptor (org.springframework.beans.factory.config) // 根据解析到的依赖bean列表创建需要的bean doResolveDependency:1287, DefaultListableBeanFactory (org.springframework.beans.factory.support) resolveDependency:1207, DefaultListableBeanFactory (org.springframework.beans.factory.support) inject:640, AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement (org.springframework.beans.factory.annotation) //根据之前放到DefaultListableBeanFactory中的beanDefinitionMap集合中的对应bean的BeanDefinition信息获得需要注入的bean列表 inject:116, InjectionMetadata (org.springframework.beans.factory.annotation) postProcessProperties:399, AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation) populateBean:1422, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) doCreateBean:594, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:323, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 1658980982 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$145) getSingleton:222, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)        
//1、 开始创建单例controller层的bean
doGetBean:321, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support) preInstantiateSingletons:879, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:878, AbstractApplicationContext (org.springframework.context.support)//这里开始创建bean refresh:550, AbstractApplicationContext (org.springframework.context.support) // 走到这里就和SpringMvc启动调用的是同一个bean初始化方法了
(注:其实这里和springmvc启动的不同之处就在用加载配置不同,加载完配置还是调用了统一的启动容器,创建bean,自动装配) refresh:141, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context) refresh:747, SpringApplication (org.springframework.boot) refreshContext:397, SpringApplication (org.springframework.boot) run:315, SpringApplication (org.springframework.boot) run:1226, SpringApplication (org.springframework.boot) run:1215, SpringApplication (org.springframework.boot) main:12, DemoApplication (com.example.mybatisdemo) 应用入口

下图是springboot启动需要自动装配的集合内容,会缓存到beanFactory 的beanDefinitionMap中
springboot的指定bean工厂是DefaultListableBeanFactory

下面来总结下整个流程:
1、 AbstractApplicationContext中的refresh()方法执行自身的
finishBeanFactoryInitialization()方法。这个方法执行了具体实例化bean的工作
2、finishBeanFactoryInitialization方法调用了bean工厂的对应方法对不同bean进行实例化。
3、spring的bean默认都是单例的,现在一单例bean的创建为例继续跟踪。spring调用了bean工厂的
preInstantiateSingletons()方法。

4、在bean工程的创建实例的方法中循环创建了工厂实现缓存好的beanDefineNames列表中对应的bean.
 
0 = "org.springframework.context.annotation.internalConfigurationAnnotationProcessor"
1 = "org.springframework.context.annotation.internalAutowiredAnnotationProcessor"
2 = "org.springframework.context.annotation.internalCommonAnnotationProcessor"
3 = "org.springframework.context.event.internalEventListenerProcessor"
4 = "org.springframework.context.event.internalEventListenerFactory"
5 = "demoApplication"
6 = "org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory"
7 = "demoController"
8 = "demoManager"
9 = "demoServiceImpl"
10 = "org.springframework.boot.autoconfigure.AutoConfigurationPackages"
11 = "cfDirectRepaymentRecordMapper"
12 = "weeklyMapper"
13 = "org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration"
14 = "propertySourcesPlaceholderConfigurer"
15 = "org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration"
16 = "websocketServletWebServerCustomizer"
17 = "org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration"
18 = "org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat"
19 = "tomcatServletWebServerFactory"
20 = "org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration"
21 = "servletWebServerFactoryCustomizer"
22 = "tomcatServletWebServerFactoryCustomizer"
23 = "org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor"
24 = "org.springframework.boot.context.internalConfigurationPropertiesBinderFactory"
25 = "org.springframework.boot.context.internalConfigurationPropertiesBinder"
26 = "org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator"
27 = "org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata"
28 = "server-org.springframework.boot.autoconfigure.web.ServerProperties"
29 = "webServerFactoryCustomizerBeanPostProcessor"
30 = "errorPageRegistrarBeanPostProcessor"
31 = "org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration"
32 = "dispatcherServlet"
33 = "spring.http-org.springframework.boot.autoconfigure.http.HttpProperties"
34 = "spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties"
35 = "org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration"
36 = "dispatcherServletRegistration"
37 = "org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration"
38 = "org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration"
39 = "taskExecutorBuilder"
40 = "applicationTaskExecutor"
41 = "spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties"
42 = "org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration"
43 = "defaultValidator"
44 = "methodValidationPostProcessor"
45 = "org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration"
46 = "error"
47 = "beanNameViewResolver"
48 = "org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration"
49 = "conventionErrorViewResolver"
50 = "org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration"
51 = "errorAttributes"
52 = "basicErrorController"
53 = "errorPageCustomizer"
54 = "preserveErrorControllerTargetClassPostProcessor"
55 = "spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties"
56 = "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration"
57 = "requestMappingHandlerAdapter"
58 = "requestMappingHandlerMapping"
59 = "welcomePageHandlerMapping"
60 = "mvcConversionService"
61 = "mvcValidator"
62 = "mvcContentNegotiationManager"
63 = "mvcPathMatcher"
64 = "mvcUrlPathHelper"
65 = "viewControllerHandlerMapping"
66 = "beanNameHandlerMapping"
67 = "routerFunctionMapping"
68 = "resourceHandlerMapping"
69 = "mvcResourceUrlProvider"
70 = "defaultServletHandlerMapping"
71 = "handlerFunctionAdapter"
72 = "mvcUriComponentsContributor"
73 = "httpRequestHandlerAdapter"
74 = "simpleControllerHandlerAdapter"
75 = "handlerExceptionResolver"
76 = "mvcViewResolver"
77 = "mvcHandlerMappingIntrospector"
78 = "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter"
79 = "defaultViewResolver"
80 = "viewResolver"
81 = "requestContextFilter"
82 = "org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration"
83 = "formContentFilter"
84 = "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidStatViewServletConfiguration"
85 = "statViewServletRegistrationBean"
86 = "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidWebStatFilterConfiguration"
87 = "webStatFilterRegistrationBean"
88 = "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration"
89 = "statFilter"
90 = "com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure"
91 = "dataSource"
92 = "spring.datasource.druid-com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties"
93 = "spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties"
94 = "org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration$HikariPoolDataSourceMetadataProviderConfiguration"
95 = "hikariPoolDataSourceMetadataProvider"
96 = "org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration"
97 = "org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker"
98 = "org.springframework.boot.autoconfigure.jdbc.DataSourceInitializationConfiguration"
99 = "dataSourceInitializerPostProcessor"
List beanNames中实际装的值
 
 public void preInstantiateSingletons() throws BeansException {
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Pre-instantiating singletons in " + this);
        }

        List<String> beanNames = new ArrayList(this.beanDefinitionNames);
        Iterator var2 = beanNames.iterator();

        while(true) {
            String beanName;
            Object bean;
            do {
                while(true) {
                    RootBeanDefinition bd;
                    do {
                        do {
                            do {
                                if (!var2.hasNext()) {
                                    var2 = beanNames.iterator();

                                    while(var2.hasNext()) {
                                        beanName = (String)var2.next();
                                        Object singletonInstance = this.getSingleton(beanName);
                                        if (singletonInstance instanceof SmartInitializingSingleton) {
                                            SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton)singletonInstance;
                                            if (System.getSecurityManager() != null) {
                                                AccessController.doPrivileged(() -> {
                                                    smartSingleton.afterSingletonsInstantiated();
                                                    return null;
                                                }, this.getAccessControlContext());
                                            } else {
                                                smartSingleton.afterSingletonsInstantiated();
                                            }
                                        }
                                    }

                                    return;
                                }

                                beanName = (String)var2.next();
                                bd = this.getMergedLocalBeanDefinition(beanName);
                            } while(bd.isAbstract());
                        } while(!bd.isSingleton());
                    } while(bd.isLazyInit());

                    if (this.isFactoryBean(beanName)) {
                        bean = this.getBean("&" + beanName);
                        break;
                    }

                    this.getBean(beanName);
                }
            } while(!(bean instanceof FactoryBean));

            FactoryBean<?> factory = (FactoryBean)bean;
            boolean isEagerInit;
            if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                SmartFactoryBean var10000 = (SmartFactoryBean)factory;
                ((SmartFactoryBean)factory).getClass();
                isEagerInit = (Boolean)AccessController.doPrivileged(var10000::isEagerInit, this.getAccessControlContext());
            } else {
                isEagerInit = factory instanceof SmartFactoryBean && ((SmartFactoryBean)factory).isEagerInit();
            }

            if (isEagerInit) {
                this.getBean(beanName);
            }
        }
    }
bean工厂中的preInstantiateSingletons()方法

5、在循环构造实例的方法中调用了从抽象父类AbstractBeanFactory继承来的this.getBean(beanName);方法。

通过细心看代码,通过工厂模式创建bean和通过类自身的构造方法创建bean的流程是不一样的,这里通过在beanName上加&符作为区分

 6、接下来看下我们自定义的一个controller类的实例化过程

  1)spring使用AbstractAutowireCapableBeanFactory工厂创建controller类实例    
  2)通过doCreateBean方法创建bean:首先创建实例对象,然后方法内调用本类的populateBean方法对内部的依赖bean进行初始化
  3)初始化过程中调用了AutowiredAnnotationBeanPostProcessor的postProcessProperties方法
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
        //首先获得所有被注解了的需要初始化的成员变量列表
        InjectionMetadata metadata = this.findAutowiringMetadata(beanName, bean.getClass(), pvs);

        try {
metadata.inject(bean, beanName, pvs);
return pvs; } catch (BeanCreationException var6) { throw var6; } catch (Throwable var7) { throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", var7); } }
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
        Collection<InjectionMetadata.InjectedElement> checkedElements = this.checkedElements;
        Collection<InjectionMetadata.InjectedElement> elementsToIterate = checkedElements != null ? checkedElements : this.injectedElements;
        InjectionMetadata.InjectedElement element;
        if (!((Collection)elementsToIterate).isEmpty()) {
            for(Iterator var6 = ((Collection)elementsToIterate).iterator(); var6.hasNext(); element.inject(target, beanName, pvs)) {
                element = (InjectionMetadata.InjectedElement)var6.next();
                if (logger.isTraceEnabled()) {
                    logger.trace("Processing injected element of bean '" + beanName + "': " + element);
                }
            }
        }

    }

 可以看到,这里具体操作是调用了AutowiredAnnotationBeanPostProcessor的内部类AutowiredFieldElement中重写抽象类InjectedElement的inject方法

private class AutowiredFieldElement extends InjectedElement {
        private final boolean required;
        private volatile boolean cached = false;
        @Nullable
        private volatile Object cachedFieldValue;

        public AutowiredFieldElement(Field field, boolean required) {
            super(field, (PropertyDescriptor)null);
            this.required = required;
        }

        protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
            Field field = (Field)this.member;
            Object value;
            if (this.cached) {
                value = AutowiredAnnotationBeanPostProcessor.this.resolvedCachedArgument(beanName, this.cachedFieldValue);
            } else {
                DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
                desc.setContainingClass(bean.getClass());
                Set<String> autowiredBeanNames = new LinkedHashSet(1);
                Assert.state(AutowiredAnnotationBeanPostProcessor.this.beanFactory != null, "No BeanFactory available");
                TypeConverter typeConverter = AutowiredAnnotationBeanPostProcessor.this.beanFactory.getTypeConverter();

                try {
                    value = AutowiredAnnotationBeanPostProcessor.this.beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
                } catch (BeansException var12) {
                    throw new UnsatisfiedDependencyException((String)null, beanName, new InjectionPoint(field), var12);
                }

                synchronized(this) {
                    if (!this.cached) {
                        if (value == null && !this.required) {
                            this.cachedFieldValue = null;
                        } else {
                            this.cachedFieldValue = desc;
                            AutowiredAnnotationBeanPostProcessor.this.registerDependentBeans(beanName, autowiredBeanNames);
                            if (autowiredBeanNames.size() == 1) {
                                String autowiredBeanName = (String)autowiredBeanNames.iterator().next();
                                if (AutowiredAnnotationBeanPostProcessor.this.beanFactory.containsBean(autowiredBeanName) && AutowiredAnnotationBeanPostProcessor.this.beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
                                    this.cachedFieldValue = new AutowiredAnnotationBeanPostProcessor.ShortcutDependencyDescriptor(desc, autowiredBeanName, field.getType());
                                }
                            }
                        }

                        this.cached = true;
                    }
                }
            }

            if (value != null) {
                ReflectionUtils.makeAccessible(field);
                field.set(bean, value);
            }

        }
    }
}

而抽象类抽象类InjectedElement的inject方法内部的实现其实只是给他的成员变量赋空值,这里调用的是子类重后的方法,方法里再次调用beanFactory创建需要赋值的成员变量实例。

所以算是递归调用beanfactory创建需要的所有bean。

 

 


 

 






猜你喜欢

转载自www.cnblogs.com/tianhaichao/p/12202446.html