Spring 源码
2024-01-24 20:07:49 1 举报
AI智能生成
Spring 源码是 Java 平台下的一个开源框架,提供了一种简单的方法来开发企业级应用程序。它的主要特点是依赖注入和面向切面编程。Spring 源码采用分层架构设计,包括核心容器、数据访问/集成、Web、AOP(面向切面编程)、工具、消息和测试模块。Spring 源码中包含了大量的设计模式,如单例模式、工厂模式、代理模式等,使得代码更加简洁、易于维护。此外,Spring 源码还提供了丰富的扩展机制,可以通过自定义 bean、拦截器等来实现个性化需求。总之,Spring 源码是一个功能强大、灵活性高的企业级应用程序开发框架。
作者其他创作
大纲/内容
Spring官网
Spring基础
分支主题
Spring核心 IOC实现
准备工作
1.注册内置PostProcess
this.reader = new AnnotatedBeanDefinitionReader(this);
internalConfigurationAnnotationProcessor
ConfigurationClassPostProcessor.class
ConfigurationClassPostProcessor.class
internalEventListenerFactory
DefaultEventListenerFactory
DefaultEventListenerFactory
internalEventListenerProcessor
EventListenerMethodProcessor
EventListenerMethodProcessor
internalAutowiredAnnotationProcessor
AutowiredAnnotationBeanPostProcessor
AutowiredAnnotationBeanPostProcessor
internalCommonAnnotationProcessor
CommonAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
internalRequiredAnnotationProcessor(已过期)
RequiredAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
AnnotationAwareOrderComparator
ContextAnnotationAutowireCandidateResolver
2.注册扫描器
this.scanner = new ClassPathBeanDefinitionScanner
3.注册配置类
register(annotatedClasses);
监听器
创建事件多播器
initApplicationEventMulticaster()
- new SimpleApplicationEventMulticaster(beanFactory)
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
ApplicationListener接口的监听器解析过程
refresh->registerListeners
将监听器的名字添加到多播器中:applicationListnerBeans
将监听器的名字添加到多播器中:applicationListnerBeans
这里思考一个问题:
为什么registerListeners注册了一遍监听器
在BeanPostProcerssor中又添加一次呢?
为什么registerListeners注册了一遍监听器
在BeanPostProcerssor中又添加一次呢?
答案:为了懒加载的漏网之鱼
在prepareBeanFactory中注册了BeanPostProcessor
在初始化后的BeanPostProcerssor中调用
ApplicationListenerDetector
将监听器添加到多播器中:applicationListeners
ApplicationListenerDetector
将监听器添加到多播器中:applicationListeners
add
AbstractApplicationContext#
applicationListeners
applicationListeners
@EventListener事件的解析过程
在创世纪Bean定义中注册了2个关于解析@EventListener
调用
EventListenerMethodProcessor.afterSingletonsInstantiated
处理@EventListner
EventListenerMethodProcessor.afterSingletonsInstantiated
处理@EventListner
add
ApplicationListenerMethodAdapter
ApplicationListenerMethodAdapter
AbstractApplicationContext#
applicationListeners
applicationListeners
PublishEnent
SimpleApplicationEventMulticaster
multiCastEnent
multiCastEnent
接口
ApplicationListener.onApplicationEvent
onApplicationEnent
直接调用
注解
ApplicationListener.onApplicationEvent
ApplicationListenerMethodAdapter
onApplicationEvent
onApplicationEvent
通过反射调用
解析BeanDefinition
invokeBeanFactoryPostProcess
注册beanPostProcessor
refresh()
registerBeanProcessors
AbstractBeanFactory.addBeanPostProcessor
invokeBeanFactoryPostProcessors
ImportAwareBeanPostProcessor
BeanPostProcessorChecker
AutowiredAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
类图
方法
bean后置处理器9处调用
注册bean创建过程
refresh()
finishBeanFactoryInitialization()
preInstantiateSingletons
getBean
doGetBean()
实例化
属性注入
循环依赖
初始化
Spring AOP实现
@EnableAspectJAutoProxy
AspectJAutoProxyRegistrar
#registerBeanDefinitions
#registerBeanDefinitions
AopConfigUtils
#registerAspectJAnnotationAutoProxyCreatorIfNecessary
#registerAspectJAnnotationAutoProxyCreatorIfNecessary
AopConfigUtils
#registerOrEscalateApcAsRequired
#registerOrEscalateApcAsRequired
AnnotationAwareAspectJAutoProxyCreator
关系结构图
AbstractAutoProxyCreator
InstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation
BeanPostProcess
postProcessAfterInitialization
SmartInstantiationAwareBeanPostProcessor
getEarlyBeanReference
bean后置处理器9次调用
注册BeanPostProcessors
refresh()
registerBeanPostProcessors
registerBeanPostProcessors
AbstractBeanFactory.addBeanPostProcessor
internalAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
解析切面(缓存通知)
createBean
resolveBeforeInstantiation
AbstractAutoProxyCreator
postProcessBeforeInstantiation
postProcessBeforeInstantiation
创建代理
doCreateBean
initalizeBean
applyBeanPostProcessorsAfterInitialization
AbstractAutoProxyCreator
postProcessAfterInitialization
postProcessAfterInitialization
调用代理
JdkDynamicAopProxy为例
Spring 事务实现
@EnableTransactionManagement
TransactionManagementConfigurationSlector
AutoProxyRegistrar
internalAutoProxyCreator
InfrastructureAdvisorAutoProxyCreator
InfrastructureAdvisorAutoProxyCreator
关系结构图
AbstractAutoProxyCreator
InstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation
SmartInstantiationAwareBeanPostProcessor
getEarlyBeanReference
BeanPostProcess
postProcessAfterInitialization
ProxyTransactionManagementConfiguration
beanName:internalTransactionAdvisor
beanClass:BeanFactoryTransactionAttributeSourceAdvisor
beanClass:BeanFactoryTransactionAttributeSourceAdvisor
TransactionAttributeSource
TransactionInterceptor
注册BeanPostProcessors
refresh()
registerBeanPostProcessors
registerBeanPostProcessors
AbstractBeanFactory.addBeanPostProcessor
internalAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
关键方法
bean的后置处理器9处调用
解析切面(缓存通知)
createBean
resolveBeforeInstantiation
AbstractAutoProxyCreator
postProcessBeforeInstantiation
postProcessBeforeInstantiation
事务切面解析
创建代理
doCreateBean
initializeBean
applyBeanPostProcessorsAfterInitialization
AbstractAutoProxyCreator
postProcessAfterInitialization
postProcessAfterInitialization
创建事务动态代理
调用代理
JdkDynamicAopProxy为例
Spring5 新特性
JDK版本升级
spring-webflux
Kotlin函数式编程
spring-jcl
支持Junit 5
0 条评论
下一页