日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

一個很有意思的Spring注入問題,你遇到過嗎?

來源: 責編: 時間:2024-03-18 09:36:48 206觀看
導讀環境:Spring5.3.231. 問題描述static interface DAO {}static class CommonDAO implements DAO {}@Configurationstatic class AppConfig { @Bean DAO dao() { return new CommonDAO() ; }}static class CommonS

環境:Spring5.3.23rwP28資訊網——每日最新資訊28at.com

1. 問題描述

static interface DAO {}static class CommonDAO implements DAO {}@Configurationstatic class AppConfig {  @Bean  DAO dao() {    return new CommonDAO() ;  }}static class CommonService {  @Resource  private DAO dao ;  @Resource  private CommonDAO commonDAO ;}try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {  context.registerBean(AppConfig.class) ;  context.registerBean(CommonService.class) ;  context.refresh() ;}

上面是基本的bean定義。在AppConfig配置類中定義了DAO bean實例,在CommonService中分別去注入DAO 接口和CommonDAO。運行上面的程序沒有問題正常。rwP28資訊網——每日最新資訊28at.com

2. 問題匯總

2.1 修改注入1

static class CommonService {  @Resource  private CommonDAO commonDAO ;}

當CommonService只注入CommonDAO時,程序既然報錯了rwP28資訊網——每日最新資訊28at.com

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.pack.main.bean_propertyvalue_inject.InterfaceAndImplInject$CommonDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801)  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357)

錯誤提示:需要CommonDAO但是容器中沒有,是不是很奇怪。rwP28資訊網——每日最新資訊28at.com

2.2. 修改注入2

static class CommonService {  @Resource  private CommonDAO dao;}

只是吧字段的名稱修改為dao,程序又正確了。這個什么原因???rwP28資訊網——每日最新資訊28at.com

2.3 修改注入3

static class CommonService {  @Resource  private CommonDAO commonDAO ;  @Resource  private DAO dao ;}

這里僅僅是修改了下字段的順序,程序又報錯了,是不是太神奇了。rwP28資訊網——每日最新資訊28at.com

2.4 修改注入4

@Configurationstatic class AppConfig {  @Bean  CommonService commonService() {    return new CommonService() ;  }  @Bean  DAO dao() {    return new CommonDAO() ;  }}static class CommonService {  @Resource  private CommonDAO commonDAO ;}

修改了CommonService bean的注冊方式,運行程序還是錯誤rwP28資訊網——每日最新資訊28at.com

2.5 修改注入5

@Configurationstatic class AppConfig {  @Bean  DAO dao() {    return new CommonDAO() ;  }  @Bean  CommonService commonService() {    return new CommonService() ;  }}

根據2.4的情況,修改注冊DAO與CommonService的順序后,程序又正確了。rwP28資訊網——每日最新資訊28at.com

3. 原因解析

當如下方式注入時rwP28資訊網——每日最新資訊28at.com

@Resourceprivate DAO dao ;@Resourceprivate CommonDAO commonDAO ;

提示:@Resource注解對應的處理器是CommonAnnotationBeanPostProcessorrwP28資訊網——每日最新資訊28at.com

這里首先要整清楚@Resource的注入方式rwP28資訊網——每日最新資訊28at.com

@Resource先根據beanName進行查找,再通過類型查找。源碼:rwP28資訊網——每日最新資訊28at.com

public class CommonAnnotationBeanPostProcessor {  protected Object autowireResource(BeanFactory factory, LookupElement element, @Nullable String requestingBeanName) {    Object resource;    if (factory instanceof AutowireCapableBeanFactory) {      AutowireCapableBeanFactory beanFactory = (AutowireCapableBeanFactory) factory;      DependencyDescriptor descriptor = element.getDependencyDescriptor();      // 判斷你當前注入屬性的名字(beanName) 在容器中是否存在。這里取反了,如果不存在時進行類型的查找      if (this.fallbackToDefaultTypeMatch && element.isDefaultName && !factory.containsBean(name)) {        resource = beanFactory.resolveDependency(descriptor, requestingBeanName, autowiredBeanNames, null);      } else {        // 存在,直接通過beanName(這里就是字段名)查找        resource = beanFactory.resolveBeanByName(name, descriptor);        autowiredBeanNames = Collections.singleton(name);      }    }    return resource;  }}

上面你知道了@Resource注解的方式注入的方式后。接下來就是查找具體的bean了,不管是通過beanName還是類型。這里演示還是按照beanName方式,接著上面的代碼rwP28資訊網——每日最新資訊28at.com

public abstract class AbstractAutowireCapableBeanFactory {  public Object resolveBeanByName(String name, DependencyDescriptor descriptor) {    return getBean(name, descriptor.getDependencyType());  }}public abstract class AbstractBeanFactory {  public <T> T getBean(String name, Class<T> requiredType) throws BeansException {    return doGetBean(name, requiredType, null, false);  }  protected <T> T doGetBean(    String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) {    // 這里就是先從單例池中獲取指定beanName是否存在,如果不存在則進行創建bean實例。    // 創建完成后將當前的實例存入單例池中。  }}

到此,DAO類型的屬性就注入成功了,接下是注入CommonDAO。注入CommonDAO由于容器中沒有對應的beanName,所有進入上面的if語句中。rwP28資訊網——每日最新資訊28at.com

public class DefaultListableBeanFactory {  public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,    @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {    // ...    Object result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);  }  public Object doResolveDependency(...) {    // ...    Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);  }  protected Map<String, Object> findAutowireCandidates(    @Nullable String beanName, Class<?> requiredType, DependencyDescriptor descriptor) {    // 通過類型查找beanNames, 當前reqiredType=CommonDAO    String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(        this, requiredType, true, descriptor.isEager());  }}public abstract class BeanFactoryUtils {  public static String[] beanNamesForTypeIncludingAncestors(      ListableBeanFactory lbf, Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {    // 通過類型查找    String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit);    return result;  }}public class DefaultListableBeanFactory {  public String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {    // 通過類型查找    String[] resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, true);    return resolvedBeanNames;  }  private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {    // 遍歷所有的BeanDefinition(這是Spring容器對每一個bena的元數據了)    for (String beanName : this.beanDefinitionNames) {      RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName) ;      // 關鍵代碼      matchFound = isTypeMatch(beanName, type, true);    }  }  protected boolean isTypeMatch(String name, ...) {    // beanName = dao    String beanName = transformedBeanName(name);    // 從單例池中獲取實例,這里肯定可以獲取,我們第一個屬性注入的就是    // DAO,所以這里就返回了CommonDAO實例    Object beanInstance = getSingleton(beanName, false);    if (beanInstance != null && beanInstance.getClass() != NullBean.class) {      // 這里肯定是實例對象,直接返回了      if (typeToMatch.isInstance(beanInstance)) {        return true;      }    }  }}

到這你應該清楚了為什么同時有DAO和CommonDAO注入時能成功了。但是當沒有DAO注入的時候為什么就錯誤呢?原因其實在上面已經給出了,你只要包裝我在注入CommonDAO時,容器中已經將DAO這個bean實例創建存入到單例池中即可。這也就是為什么上面我們調整合理的順序后就能注入成功。還有就是你可以將CommonDAO的字段名稱改成與DAO bean的beanName一致也是可以的。rwP28資訊網——每日最新資訊28at.com

以上是本篇文章的全部內容,希望對你有幫助。rwP28資訊網——每日最新資訊28at.com

完畢?。?!rwP28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-76486-0.html一個很有意思的Spring注入問題,你遇到過嗎?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 沒看過ReentrantLock源碼,別說精通Java并發編程

下一篇: JQuery 4.0震撼發布:這是復興還是告別?

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 米脂县| 伊通| 五华县| 永新县| 莫力| 弥勒县| 兴隆县| 大同市| 比如县| 鹰潭市| 卓尼县| 郑州市| 苏尼特右旗| 平陆县| 禹城市| 安宁市| 龙海市| 濮阳市| 凤凰县| 永善县| 太仆寺旗| 仙游县| 阿克苏市| 锦州市| 德江县| 微博| 吉木萨尔县| 彩票| 白玉县| 宝丰县| 林西县| 清涧县| 五华县| 吉林市| 县级市| 龙口市| 隆德县| 滨州市| 大足县| 马公市| 原平市|