1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans
- In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
- A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
- Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
Spring创建Bean的过程
- AbstractApplicationContext.prepareRefresh():环境准备
- AbstractApplicationContext.obtainFreshBeanFactory():创建beanFactory
- GenericApplicationContext.getBeanFactory()
- AbstractApplicationContext.prepareBeanFactory():beanFactory准备
- AnnotationConfigEmbeddedWebApplicationContext.postProcessBeanFactory()
- EmbeddedWebApplicationContext.postProcessBeanFactory():设置beanPostProcessor为WebApplicationContextServletContextAwareProcessor对象;
- AbstractApplicationContext.invokeBeanFactoryPostProcessors()
- PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors()
- AbstractApplicationContext.registerBeanPostProcessors() 注册beanPostProcessor;
- PostProcessorRegistrationDelegate.registerBeanPostProcessors()
- AbstractApplicationContext.initMessageSource():初始化容器中的messageSource;
- AbstractApplicationContext.initApplicationEventMulticaster():初始化事件分发器;
- EmbeddedWebApplicationContext.onRefresh()
- EmbeddedWebApplicationContext.createEmbeddedServletContainer()
- AbstractApplicationContext.registerListeners():注册事件监听器
- AbstractApplicationContext.finishBeanFactoryInitialization():
- DefaultListableBeanFactory.preInstantiateSingletons();
-
AbstractBeanFactory.getBean()
-
AbstractBeanFactory.doGetBean()
-
transformedBeanName():转化BeanName
-
getMergedLocalBeanDefinition():合并RootBeanDefinition
-
beforePrototypeCreation()/afterPrototypeCreation()解决循环依赖
-
创建实例:
-
AbstractAutowireCapableBeanFactory.createBean();
-
AbstractAutowireCapableBeanFactory.doCreateBean();
-
AbstractAutowireCapableBeanFactory.createBeanInstance();
-
AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod();工厂方法创建对象;
-
AbstractAutowireCapableBeanFactory.autowireConstructor();推断构造函数;
-
AbstractAutowireCapableBeanFactory.instantiateBean();无参构造函数;
-
属性注入populateBean()
-
初始化:
- invokeAwareMethods()
- BeanPostProcessor.postProcessBeforeInstantiation()
- invokeInitMethods()
- 1、判断bean是否继承了InitializingBean,如果继承接口,执行afterPropertiesSet()方法,
- 2、获得是否设置了init-method属性,如果设置了,就执行设置的方法
- BeanPostProcessor.postProcessAfterInstantiation() 钩子
-
类型转化convertIfNecessary()
-
- DefaultListableBeanFactory.preInstantiateSingletons();
- AbstractApplicationContext.finishRefresh():refresh完成
Spring销毁Bean的过程
- LiveBeansView.unregisterApplicationContext():卸载注册的IOC对象
- AbstractApplicationContext.destroyBeans():销毁bean
- DefaultSingletonBeanRegistry.destroySingletons():销毁单粒bean;
- DefaultSingletonBeanRegistry.destroySingleton()
- DefaultSingletonBeanRegistry.destroyBean()
- DisposableBean.destroy()
- DefaultSingletonBeanRegistry.destroyBean()
- DefaultSingletonBeanRegistry.destroySingleton()
- DefaultSingletonBeanRegistry.destroySingletons():销毁单粒bean;
- AbstractApplicationContext.closeBeanFactory():销毁beanFactory对象
Mybatis执行SQL过程
- MapperProxy.invoke()
- MapperMethod.execute()
- MapperMethod.executeForMany()
- SqlSessionTemplate.selectList()
- SqlSessionTemplate.invoke()
- DefaultSqlSession.selectList()
- this.executor.query()
- this.query()先判断缓存是否为空,若为空
- this.delegate.query()
- BaseExecutor.query()
- BaseExecutor.queryFromDatabase()
- BaseExecutor.doQuery()
- RoutingStatementHandler.query()
- PreparedStatementHandler.query()
- PreparedStatementLogger.invoke()
- StatementFacade.invoke()
- PreparedStatement.execute()
- PreparedStatement.executeInternal()
- MySQLConnection.execSQL()
- MysqlIO.sqlQueryDirect()
- MysqlIO.sendCommand()
- MysqlIO.send()
- DefaultResultSetHandler.handleResultSets()
- DefaultResultSetHandler.handleResultSet()
@EnableAutoConfiguration注解
- @EnableAutoConfiguration注解中import了EnableAutoConfigurationImportSelector类。
- SpringBoot启动过程的函数调用:
- SpringApplication.run()
- SpringApplication.createApplicationContext()注册ConfigurationClassPostProcessor到Spring容器中;
- AnnotatedBeanDefinitionReader.AnnotatedBeanDefinitionReader()构造函数;
- AnnotationConfigUtils.registerAnnotationConfigProcessors()
- RootBeanDefinition.RootBeanDefinition();
- AnnotationConfigUtils.registerPostProcessor();
- RootBeanDefinition.RootBeanDefinition();
- AnnotationConfigUtils.registerAnnotationConfigProcessors()
- AnnotatedBeanDefinitionReader.AnnotatedBeanDefinitionReader()构造函数;
- SpringApplication.refreshContext()调用
- AbstractApplicationContext.refresh()
- AbstractApplicationContext.invokeBeanFactoryPostProcessors()
- AutoConfigurationImportSelector.selectImports()
- AutoConfigurationImportSelector.getCandidateConfigurations()
- SpringFactoriesLoader.loadFactoryNames()加载spring-boot-autoconfigure.jar/META-INF/spring.factories文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的value;
- AbstractApplicationContext.refresh()