java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot处理@Resource

Spring Boot如何处理@Resource示例分析

作者:子瞻

这篇文章主要为大家介绍了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的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文