@Bean方法解析流程概述
2020-05-30 17:51:23 0 举报
Spring内全配置类下的@Bean方法解析流程概述
作者其他创作
大纲/内容
调用目标对象目标方法
调用目标方法的代理方法
开始创建bBean
设置bBean()方法对象进入线程副本currentlyInvokedFactoryMethod
第二种案例
完成创建aBean
容器中是否有bBean
开始创建aBean
注意:font color=\"#000000\
注意方法顺序:span style=\"font-size: inherit;\
ConfigurationClassEnhancer.BeanMethodInterceptor#isCurrentlyInvokedFactoryMethod里:private boolean isCurrentlyInvokedFactoryMethod(Method method) { Method currentlyInvoked = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod(); return (currentlyInvoked != null && method.getName().equals(currentlyInvoked.getName()) &&font color=\"#000000\
这里要注意:Spring不会再去设置aBean()方法对象进入线程副本currentlyInvokedFactoryMethod里
不相同
@Configuration@ComponentScan(basePackages = {\"xxx\"})public class AppConfig { @Bean public BBean bBean(){ aBean(); return new BBean(); } @Bean public ABean aBean(){ return new ABean(); }}
ConfigurationClassEnhancer.BeanMethodInterceptor#intercept里:font color=\"#ff0000\
执行过程
设置beanName进入singletonsCurrentlyInCreation集合
无
SimpleInstantiationStrategy#instantiate里:currentlyInvokedFactoryMethod.set(factoryMethod);
font color=\"#000000\
代码位置
@Configuration@ComponentScan(basePackages = {\"xxx\"})public class AppConfig { @Bean public ABean aBean(){ return new ABean(); } @Bean public BBean bBean(){ aBean(); return new BBean(); }}
发现目标方法里调用了aBean()
执行流程如下
设置aBean()方法对象进入线程副本currentlyInvokedFactoryMethod
比较当前执行方法对象和线程副本中存储的方法对象
这里要注意:font color=\"#ff8000\
第一种案例
相同
容器中是否有aBean
有
程序继续向下执行
不进行任何与aBean实例化相关操作
情况1
bBean完成实例化
@Bean方法解析流程概述

收藏

收藏
0 条评论
下一页