環境:Spring5.3.23
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。運行上面的程序沒有問題正常。
static class CommonService { @Resource private CommonDAO commonDAO ;}
當CommonService只注入CommonDAO時,程序既然報錯了
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但是容器中沒有,是不是很奇怪。
static class CommonService { @Resource private CommonDAO dao;}
只是吧字段的名稱修改為dao,程序又正確了。這個什么原因???
static class CommonService { @Resource private CommonDAO commonDAO ; @Resource private DAO dao ;}
這里僅僅是修改了下字段的順序,程序又報錯了,是不是太神奇了。
@Configurationstatic class AppConfig { @Bean CommonService commonService() { return new CommonService() ; } @Bean DAO dao() { return new CommonDAO() ; }}static class CommonService { @Resource private CommonDAO commonDAO ;}
修改了CommonService bean的注冊方式,運行程序還是錯誤
@Configurationstatic class AppConfig { @Bean DAO dao() { return new CommonDAO() ; } @Bean CommonService commonService() { return new CommonService() ; }}
根據2.4的情況,修改注冊DAO與CommonService的順序后,程序又正確了。
當如下方式注入時
@Resourceprivate DAO dao ;@Resourceprivate CommonDAO commonDAO ;
提示:@Resource注解對應的處理器是CommonAnnotationBeanPostProcessor
這里首先要整清楚@Resource的注入方式
@Resource先根據beanName進行查找,再通過類型查找。源碼:
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方式,接著上面的代碼
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語句中。
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一致也是可以的。
以上是本篇文章的全部內容,希望對你有幫助。
完畢?。?!
本文鏈接:http://www.www897cc.com/showinfo-26-76486-0.html一個很有意思的Spring注入問題,你遇到過嗎?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com