Spring脑图
2024-03-14 16:06:14 0 举报
AI智能生成
Java知识点脑图
作者其他创作
大纲/内容
Spring笔记
Spring官网
作者:青水
Spring IOC 核心实现
准备工作
1. 注册内置的PostProcessor
this.reader = new AnnotatedBeanDefinitionReader(this)
internalConfigurationAnnotationProcessor ConfigurationClassPostProcessor
internalAutowiredAnnotationProcessor AutowiredAnnotationBeanPostProcessor
internalCommonAnnotationProcessor CommonAnnotationBeanPostProcessor
internalRequiredAnnotationProcessor RequiredAnnotationBeanPostProcessor
AnnotationAwareOrderComparator
ContextAnnotationAutowireCandidateResolver
internalEventListenerFactory DefaultEventListenerFactory
internalEventListenerProcessorEventListenerMethodProcessor
2. 注册扫描器
this.scanner = new ClassPathBeanDefinitionScanner(this)
3. 注册配置类
register(配置类.class)
refresh()
链接
执行BeanFactoryPostProcessor
解析BeanDefinition
invokeBeanFactoryPostProcessors(beanFactory)
eg:ConfigurationClassPostProcessor
注册BeanPostProcessor
refresh() registerBeanPostProcessors
AbstractBeanFactory.addBeanPostProcessor
invokeBeanFactoryPostProcessors
BeanPostProcessorChecker
AutowiredAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
bean后置处理器的9处调用(bean创建过程 流程图里面有逻辑)
Bean创建过程
refresh()
finishBeanFactoryInitialization(beanFactory)
DefaultListableBeanFactory#preInstantiateSingletons
getBean()
doGetbean()
实例化
属性注入
注入时 出现循环依赖
初始化
监听器
创建事件多播器
initApplicationEventMulticaster()
new SimpleApplicationEventMulticaster(beanFactory)
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
ApplicationListener接口的监听器解析过程
refresh() -> registerListeners
这里思考一个问题:为什么registerListeners注册了一遍监听器在,BeanPostProcerssor中又添加一次呢?
答案:为了懒加载的漏网之鱼!
注册ApplicationListenerDetector 这个BeanPostProcessor进行监听
@EventListener注解的解析过程
AnnotatedBeanDefinitionReader创建时注册bean
调用 EventListenerMethodProcessor#afterSingletonsInstantiated来处理 @EventListener注解
ApplicationContext.addApplicationListener(applicationListener)
publishEvent
入口 SimpleApplicationEventMulticaster#multicastEvent
实现ApplicationListener 接口
直接调用
ApplicationListener#onApplicationEvent 具体事件
注解@EventListener的方式
代理调用
ApplicationListenerMethodAdapter#onApplicationEvent
反射调用
this.method.invoke(bean, args)
Spring AOP 实现
注册自动代理功能
@EnableAspectJAutoProxy
AspectJAutoProxyRegistrar#registerBeanDefinitions
AopConfigUtils#registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry)
关系结构图
AbstractAutoProxyCreator
InstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation
BeanPostProcessor
postProcessAfterInitialization
SmartInstantiationAwareBeanPostProcessor
getEarlyBeanReference
AOP代理后置处理器的9处调用
注册自动代理后置处理器
注册后置处理(AnnotationAwareAspectJAutoProxyCreator)
AbstractApplicationContext#registerBeanPostProcessors
ConfigurableBeanFactory#addBeanPostProcessor
org.springframework.aop.config.internalAutoProxyCreatorAnnotationAwareAspectJAutoProxyCreator
关键方法
解析切面
doCreateBean
AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
AbstractAutoProxyCreator #postProcessBeforeInstantiation
对bean创建动态代理
doCreateBean
AbstractAutowireCapableBeanFactory#initializeBean
BeanPostProcessor #postProcessAfterInitialization
AbstractAutoProxyCreator #postProcessAfterInitialization
SmartInstantiationAwareBeanPostProcessor#getEarlyBeanReference
AbstractAutoProxyCreator #getEarlyBeanReference
执行动态代理
org.springframework.aop.framework.AopProxy
org.springframework.aop.framework.JdkDynamicAopProxy
org.springframework.aop.framework.CglibAopProxy
Spring 事务实现
@EnableTransactionManagement
注册Selector TransactionManagementConfigurationSelector
AutoProxyRegistrar
org.springframework.aop.config.internalAutoProxyCreator
类结构
和 @EnableAspectJAutoProxy的关系
AbstractAutoProxyCreator 同上AOP逻辑
InstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation
BeanPostProcessor
postProcessAfterInitialization
SmartInstantiationAwareBeanPostProcessor
getEarlyBeanReference
@Configuration配置类 ProxyTransactionManagementConfiguration
org.springframework.transaction.config.internalTransactionAdvisor
TransactionAttributeSource
TransactionInterceptor
解析切面(同上AOP)
事务切面解析
创建动态代理(同上AOP)
创建事务的动态代理
调用动态代理(同上AOP)
调用事务的实现
Spring 事务实现
@EnableTransactionManagement
注册Selector TransactionManagementConfigurationSelector
AutoProxyRegistrar
org.springframework.aop.config.internalAutoProxyCreator
类结构
和 @EnableAspectJAutoProxy的关系
AbstractAutoProxyCreator 同上AOP逻辑
InstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation
BeanPostProcessor
postProcessAfterInitialization
SmartInstantiationAwareBeanPostProcessor
getEarlyBeanReference
@Configuration配置类 ProxyTransactionManagementConfiguration
org.springframework.transaction.config.internalTransactionAdvisor
TransactionAttributeSource
TransactionInterceptor
解析切面(同上AOP)
事务切面解析
创建动态代理(同上AOP)
创建事务的动态代理
调用动态代理(同上AOP)
调用事务的实现
Spring5新特性
JDK版本升级
spring-webflux
Kotlin函数式编程
spring-jcl
支持junit5
0 条评论
下一页