Spring 源码解析
2021-12-13 13:59:35 187 举报
AI智能生成
Spring 源码
作者其他创作
大纲/内容
SpringIOC
Spring基础知识
SpringIOC 加载流程图,详见链接
this()
调用用父类构造函数,初始化BeanFactory
this.beanFactory = new DefaultListableBeanFactory();
初始化Bean定义扫描器并注册内置PostProcessor
this.reader = new AnnotatedBeanDefinitionReader(this);
用户处理条件表达式计算 @Condition
设置IOC容器ApplicationContext对象
设置Bean工厂对象
设置环境对象
设置资源加载对象
设置类加载器对象
注册一些配置的后置处理器
internalConfigurationAnnotationProcessor
ConfigurationAnnotationProcessor
ConfigurationAnnotationProcessor
internalAutowiredAnnotationProcessor
AutowiredAnnotationBeanPostProcessor
AutowiredAnnotationBeanPostProcessor
子主题
internalCommonAnnotationProcessor
CommonAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
internalEventListenerFactory
DefaultEventListenerFactory
DefaultEventListenerFactory
internalEventListenerProcessor
EventListenerMethodProcessor
EventListenerMethodProcessor
internalRequiredAnnotationProcessor
RequiredAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
AnnotationAwareOrderComparator
ContextAnnotationAutowireCandidateResolver
初始化 classPath类型的 bean定义扫描器
this.scanner = new ClassPathBeanDefinitionScanner(this);
register(componentClasses)
大致逻辑如
通过AnnotatedGenericBeanDefinition的构造方法,获得配置类的BeanDefinition
判断需不需要跳过注册,Spring中有一个@Condition注解,如果不满足条件,就会跳过这个类的注册
解析作用域,如果没有设置的话,默认为单例
获得BeanName
解析通用注解并填充到 AnnotatedGenericBeanDefinition-Lazy,Primary,DependsOn,Role,Description
限定符处理,不是特指@Qualifier注解,也有可能是Primary,或者是Lazy,或者是其他
把AnnotatedGenericBeanDefinition数据结构和beanName封装到一个对象中(方便传参)
注册,最终会调用 DefaultListableBeanFactory 中的 registerBeanDefinition() 方法
refresh() 刷新容器
其实里面就13个方法
在后面进行分类解析
其实里面就13个方法
在后面进行分类解析
准备刷新上下文环境
prepareBeanFactory(beanFactory)
prepareBeanFactory(beanFactory)
告诉子类初始化Bean工厂(MVC),获取Bean工厂
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()
对Bean工厂进行填充属性
prepareBeanFactory(beanFactory)
prepareBeanFactory(beanFactory)
空方法,留给子类实现
postProcessBeanFactory(beanFactory)
postProcessBeanFactory(beanFactory)
调用Bean工厂的后置处理器(内置+自定义)
该方法去还会实例化几个内置bean
invokeBeanFactoryPostProcessors(beanFactory)
该方法去还会实例化几个内置bean
invokeBeanFactoryPostProcessors(beanFactory)
流程图
注册Bean的后置处理器
registerBeanPostProcessors(beanFactory)
registerBeanPostProcessors(beanFactory)
beanFactory.addBeanPostProcessor(postProcessor)
ApplicationContextAwareProcessor
ApplicationListenerDetector
invokeBeanFactoryPostProcessors
AutowireAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
初始化国际资源
initMessageSource()
initMessageSource()
蓝色部分
监听器SpringListeners
监听器SpringListeners
创建事件多播器
initApplicationEventMulticaster()
initApplicationEventMulticaster()
空方法,留给子类实现
onRefresh()
onRefresh()
将事件监听器注册到多播器上
registerListeners()
registerListeners()
实例化懒加载单例Bean的
Bean的创建过程(生命周期)
finishBeanFactoryInitialization(beanFactory)
Bean的创建过程(生命周期)
finishBeanFactoryInitialization(beanFactory)
冻结所有BeanDefinition,初始化非懒加载单例Bean
beanFactory.freezeConfiguration()
beanFactory.preInstantiateSingletons();
beanFactory.freezeConfiguration()
beanFactory.preInstantiateSingletons();
getBean() =>
doGetBean() =>
createBean() =>
doCreateBean()
doGetBean() =>
createBean() =>
doCreateBean()
实例化
createBeanInstance()
createBeanInstance()
实现赋值
populateBean()
populateBean()
初始化
initializeBean()
initializeBean()
加入单例池
addSingletonFactory()
addSingletonFactory()
容器刷新
finishRefresh()
finishRefresh()
清除元数据缓冲
resetCommonCaches()
resetCommonCaches()
SpringAOP
@EnableAspectJAutoProxy
开启AOP
开启AOP
AspectJAutoProxyRegistrar.class
=> registerBeanDefinitions()
=> registerBeanDefinitions()
AopConfigUtils
registerAspectJAnnotationAutoProxyCreatorIfNecessary()
registerAspectJAnnotationAutoProxyCreatorIfNecessary()
AopConfigUtils 注册BeanDefinition
registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source)
registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source)
注册了一个 BeanDefinition
AnnotationAwareAspectJAutoProxyCreator
继承了AbstractAutoProxyCreator.class
AnnotationAwareAspectJAutoProxyCreator
继承了AbstractAutoProxyCreator.class
结构关系图
AbstractAutoProxyCreator.class
SpirngAOP 最核心的类
SpirngAOP 最核心的类
SmartInstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation()
getEarlyBeanReference()
InstantiationAwareBeanPostProcessor
BeanPostProcessor
postProcessAfterInstantiation()
注册BeanPostProcessor
处理AOP
处理AOP
刷新IOC容器
AnnotationConfigApplicationContext
refresh()
AnnotationConfigApplicationContext
refresh()
注册Bean的后置处理器
AnnotationConfigApplicationContext
registerBeanPostProcessors()
AnnotationConfigApplicationContext
registerBeanPostProcessors()
注册Bean的后置处理器
PostProcessorRegistrationDelegate
registerBeanPostProcessors()
PostProcessorRegistrationDelegate
registerBeanPostProcessors()
注册Bean的后置处理器
AbstractBeanFactory
addBeanPostProcessor()
AbstractBeanFactory
addBeanPostProcessor()
org.springframework.aop.config.internalAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
解析切面
将Advice加入缓存
将Advice加入缓存
实例化懒加载单例Bean的
AnnotationConfigApplicationContext
finishBeanFactoryInitialization()
AnnotationConfigApplicationContext
finishBeanFactoryInitialization()
初始化所有的非懒加载单例Bean
DefaultListableBeanFactory
preInstantiateSingletons()
DefaultListableBeanFactory
preInstantiateSingletons()
空壳方法,无实现,仅调用doCreatBean
AbstractBeanFactory
getBean()
AbstractBeanFactory
getBean()
创建Bean
AbstractBeanFactory
doCreatBean()
AbstractBeanFactory
doCreatBean()
创建Bean
AbstractAutowireCapableBeanFactory
creatBean()
AbstractAutowireCapableBeanFactory
creatBean()
让BeanPostProcessors有机会返回一个代理而不是目标bean实例
AbstractAutowireCapableBeanFactory
resolveBeforeInstantiation()
AbstractAutowireCapableBeanFactory
resolveBeforeInstantiation()
进行后置处理解析切面
AbstractAutowireCapableBeanFactory
postProcessBeforeInstantiation()
AbstractAutowireCapableBeanFactory
postProcessBeforeInstantiation()
Spring解析切面流程图详见链接
事务切面解析
接口方式切面解析
AbstractAdvisorAutoProxyCreator
findEligibleAdvisors()
AbstractAdvisorAutoProxyCreator
findEligibleAdvisors()
接口方式切面解析
AbstractAdvisorAutoProxyCreator
即:super.findCandidateAdvisors()
AbstractAdvisorAutoProxyCreator
即:super.findCandidateAdvisors()
找缓存中是否有,有说明被解析过,直接返回
循环所有的beanName判断名字是否符合
是否正在创建
调用:this.beanFactory.getBean(name, Advisor.class)
创建出 BeanFactoryTransactionAttributeSourceAdvisor
创建出 BeanFactoryTransactionAttributeSourceAdvisor
组装advisors返回并加入缓存
创建代理
CGLIB 或 JDK
CGLIB 或 JDK
创建Bean
AbstractBeanFactory
doCreatBean()
AbstractBeanFactory
doCreatBean()
进行对象初始化操作(在这里可能生成代理对象)
AbstractAutowireCapableBeanFactory
initializeBean()
AbstractAutowireCapableBeanFactory
initializeBean()
调用的bean的后置处理器的postProcessorsBeforeInitialization方法
AbstractAutowireCapableBeanFactory
applyBeanPostProcessorsBeforeInitialization()
AbstractAutowireCapableBeanFactory
applyBeanPostProcessorsBeforeInitialization()
Bean后置处理器
AbstractAdvisingBeanPostProcessor
postProcessAfterInitialization()
AbstractAdvisingBeanPostProcessor
postProcessAfterInitialization()
创建代理流程详见链接图解
调用代理
JDK代理为例
JDK代理为例
调用动态代理
ProxyFactory
getProxy()
ProxyFactory
getProxy()
调用动态代理
JdkDynamicAopProxy => getProxy()
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
JdkDynamicAopProxy => getProxy()
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
Spring 事务
开启事务,导入组件
@EnableTransactionManagement
@EnableTransactionManagement
TransactionManagementConfigurationSelector
String[] selectImports(AdviceMode adviceMode)
String[] selectImports(AdviceMode adviceMode)
AutoProxyRegistrar
internalAutoProxyCreator
infrastructureAdvisiorAutoProxyCreator
infrastructureAdvisiorAutoProxyCreator
AbstractAdvisorAutoProxyCreator
ProxyTransactionManagementConfiguration
internalTransactionAdvisor
BeanFactoryTransactionAttributeSourceAdvisor
BeanFactoryTransactionAttributeSourceAdvisor
TransactionAttributeSource
TransactionInterceptor
注册BeanPostProcessor
详见IOC
详见IOC
AOP注册Bean后置处理器那条线
注册Bean的后置处理器
registerBeanPostProcessors(beanFactory)
registerBeanPostProcessors(beanFactory)
beanFactory.addBeanPostProcessor(postProcessor)
internalAutoProxyCreator
infrastructureAdvisiorAutoProxyCreator
infrastructureAdvisiorAutoProxyCreator
解析切面
AOP事务解析切面那条线
创建代理
AOP创建代理那条线
Bean后置处理器
AbstractAdvisingBeanPostProcessor
postProcessAfterInitialization()
AbstractAdvisingBeanPostProcessor
postProcessAfterInitialization()
事务创建代理
根据事务切点匹配命中的方法
TransactionAttributeSourcePointcut
matches()
TransactionAttributeSourcePointcut
matches()
判断是否有@Transactional注解
getTransactionAttributeSource()
getTransactionAttributeSource()
解析过直接返回true
解析注解
加入事务的描述属性中
加入缓存
返回true
调用代理
AOP调用代理
事务的传播机制
SpringMVC
一张流程图全了解
Spring容器与pringMVC容器的关系
0 条评论
下一页