Spring Boot如何处理@Resource示例分析
先造一个测试用例
public class TransactionServiceTest {
@Resource
private IQrcodeAdScheduleService qrcodeAdScheduleService;
}启动Spring Boot调用栈信息

图1
解析@Resource对应的bean信息
由上图可知,在创建完bean实例后,通过applyMergedBeanDefinitionPostProcessors()修改beanDefinition结构(针对这种场景可以理解为解析@Resource对应的bean信息)
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
//执行CommonAnnotationBeanPostProcessor类postProcessMergedBeanDefinition()
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
}
}
}
图2
CommonAnnotationBeanPostProcessor
有图2可知,处理@Resource的PostProcessor是“CommonAnnotationBeanPostProcessor”,然后看一下CommonAnnotationBeanPostProcessor的部分细节:
private InjectionMetadata buildResourceMetadata(final Class<?> clazz) {
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>();
Class<?> targetClass = clazz;
do {
final LinkedList<InjectionMetadata.InjectedElement> currElements =
new LinkedList<>();
ReflectionUtils.doWithLocalFields(targetClass, field -> {
if (webServiceRefClass != null && field.isAnnotationPresent(webServiceRefClass)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("@WebServiceRef annotation is not supported on static fields");
}
currElements.add(new WebServiceRefElement(field, field, null));
}
else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("@EJB annotation is not supported on static fields");
}
currElements.add(new EjbRefElement(field, field, null));
}
//解析@Resource.class
else if (field.isAnnotationPresent(Resource.class)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("@Resource annotation is not supported on static fields");
}
if (!ignoredResourceTypes.contains(field.getType().getName())) {
currElements.add(new ResourceElement(field, field, null));
}
}
});
}上面的代码块出现了期待已久的“Resource.class”关键字,我们就放心了。
我们再回顾一下,
其流程是这样的:在AbstractAutowireCapableBeanFactory.populateBean()->ibp.postProcessPropertyValue()->CommonAnnotationBeanPostProcessor.postProcessPropertyValue()去实例化@Resource作用的bean;
除了和处理@Autowired不是一个PostProcessor(处理@AutoWireds是用这个“AutowiredAnnotationBeanPostProcessor”PostProcessor)其他处理流程和@Autowired的处理流程一毛一样啊!
以上就是Spring Boot如何处理@Resource示例分析的详细内容,更多关于Spring Boot处理@Resource的资料请关注脚本之家其它相关文章!
相关文章
Java concurrency线程池之线程池原理(一)_动力节点Java学院整理
这篇文章主要为大家详细介绍了Java concurrency线程池之线程池原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-06-06
基于Spring Boot应用ApplicationEvent案例场景
这篇文章主要介绍了基于Spring Boot应用ApplicationEvent,利用Spring的机制发布ApplicationEvent和监听ApplicationEvent,需要的朋友可以参考下2023-03-03
Java利用HttpClient模拟POST表单操作应用及注意事项
本文主要介绍JAVA中利用HttpClient模拟POST表单操作,希望对大家有所帮助。2016-04-04
SpringBoot中Mybatis + Druid 数据访问的详细过程
Spring Boot 底层都是采用 SpringData 的方式进行统一处理各种数据库,SpringData也是Spring中与SpringBoot、SpringCloud 等齐名的知名项目,下面看下SpringBoot Mybatis Druid数据访问的详细过程,感兴趣的朋友一起看看吧2021-11-11


最新评论