Spring?Boot如何處理@Resource示例分析
先造一個(gè)測(cè)試用例
public class TransactionServiceTest {
@Resource
private IQrcodeAdScheduleService qrcodeAdScheduleService;
}啟動(dòng)Spring Boot調(diào)用棧信息

圖1
解析@Resource對(duì)應(yīng)的bean信息
由上圖可知,在創(chuàng)建完bean實(shí)例后,通過applyMergedBeanDefinitionPostProcessors()修改beanDefinition結(jié)構(gòu)(針對(duì)這種場(chǎng)景可以理解為解析@Resource對(duì)應(yīng)的bean信息)
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
//執(zhí)行CommonAnnotationBeanPostProcessor類postProcessMergedBeanDefinition()
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
}
}
}
圖2
CommonAnnotationBeanPostProcessor
有圖2可知,處理@Resource的PostProcessor是“CommonAnnotationBeanPostProcessor”,然后看一下CommonAnnotationBeanPostProcessor的部分細(xì)節(jié):
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));
}
}
});
}上面的代碼塊出現(xiàn)了期待已久的“Resource.class”關(guān)鍵字,我們就放心了。
我們?cè)倩仡櫼幌拢?br />其流程是這樣的:在AbstractAutowireCapableBeanFactory.populateBean()->ibp.postProcessPropertyValue()->CommonAnnotationBeanPostProcessor.postProcessPropertyValue()去實(shí)例化@Resource作用的bean;
除了和處理@Autowired不是一個(gè)PostProcessor(處理@AutoWireds是用這個(gè)“AutowiredAnnotationBeanPostProcessor”PostProcessor)其他處理流程和@Autowired的處理流程一毛一樣?。?/p>
以上就是Spring Boot如何處理@Resource示例分析的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot處理@Resource的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)\和\\\\的理解
這篇文章主要介紹了淺談關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)\和\\\\的理解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作
這篇文章主要介紹了在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
spring boot使用thymeleaf跳轉(zhuǎn)頁(yè)面實(shí)例代碼
本篇文章主要介紹了spring boot使用thymeleaf跳轉(zhuǎn)頁(yè)面,實(shí)例介紹了thymeleaf的原理和介紹,有興趣的可以了解一下。2017-04-04
Spring?Boot?3?整合?MinIO?實(shí)現(xiàn)分布式文件存儲(chǔ)的全過程
本文介紹了如何使用SpringBoot3和MinIO實(shí)現(xiàn)分布式文件存儲(chǔ),通過MinIO的分布式對(duì)象存儲(chǔ)系統(tǒng),可以解決傳統(tǒng)單機(jī)文件存儲(chǔ)方案在面對(duì)大規(guī)模數(shù)據(jù)和高并發(fā)訪問時(shí)的不足,文章詳細(xì)講解了MinIO的安裝、配置和使用,感興趣的朋友一起看看吧2025-03-03
Spring Boot使用Spring Mail發(fā)送郵件
在現(xiàn)代應(yīng)用程序中,郵件通知是一種非常常見的需求,無論是用戶注冊(cè)成功后的歡迎郵件,還是系統(tǒng)異常時(shí)的報(bào)警郵件,都離不開郵件服務(wù)的支持,Spring Boot提供了簡(jiǎn)便的方式Spring Mail來集成郵件發(fā)送功能,使得開發(fā)者能夠快速地為應(yīng)用添加郵件發(fā)送能力2025-05-05
springboot使用jasypt加密庫(kù)實(shí)現(xiàn)數(shù)據(jù)庫(kù)加解密示例代碼
這篇文章主要給大家介紹了關(guān)于springboot使用jasypt加密庫(kù)實(shí)現(xiàn)數(shù)據(jù)庫(kù)加解密的相關(guān)資料,Jasypt是一個(gè)用于配置文件加密的Java庫(kù),它可以用來加密和解密配置文件中的敏感信息,如數(shù)據(jù)庫(kù)密碼、API?密鑰等,需要的朋友可以參考下2024-04-04

