一個很有意思的Spring注入問題,你遇到過嗎?
環(huán)境:Spring5.3.23
1. 問題描述
static interface DAO {}
static class CommonDAO implements DAO {}
@Configuration
static 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。運(yùn)行上面的程序沒有問題正常。
2. 問題匯總
2.1 修改注入1
static class CommonService {
@Resource
private CommonDAO commonDAO ;
}
當(dāng)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但是容器中沒有,是不是很奇怪。
2.2. 修改注入2
static class CommonService {
@Resource
private CommonDAO dao;
}
只是吧字段的名稱修改為dao,程序又正確了。這個什么原因???
2.3 修改注入3
static class CommonService {
@Resource
private CommonDAO commonDAO ;
@Resource
private DAO dao ;
}
這里僅僅是修改了下字段的順序,程序又報錯了,是不是太神奇了。
2.4 修改注入4
@Configuration
static class AppConfig {
@Bean
CommonService commonService() {
return new CommonService() ;
}
@Bean
DAO dao() {
return new CommonDAO() ;
}
}
static class CommonService {
@Resource
private CommonDAO commonDAO ;
}
修改了CommonService bean的注冊方式,運(yùn)行程序還是錯誤
2.5 修改注入5
@Configuration
static class AppConfig {
@Bean
DAO dao() {
return new CommonDAO() ;
}
@Bean
CommonService commonService() {
return new CommonService() ;
}
}
根據(jù)2.4的情況,修改注冊DAO與CommonService的順序后,程序又正確了。
3. 原因解析
當(dāng)如下方式注入時
@Resource
private DAO dao ;
@Resource
private CommonDAO commonDAO ;
提示:@Resource注解對應(yīng)的處理器是CommonAnnotationBeanPostProcessor
這里首先要整清楚@Resource的注入方式
@Resource先根據(jù)beanName進(jìn)行查找,再通過類型查找。源碼:
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();
// 判斷你當(dāng)前注入屬性的名字(beanName) 在容器中是否存在。這里取反了,如果不存在時進(jìn)行類型的查找
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是否存在,如果不存在則進(jìn)行創(chuàng)建bean實例。
// 創(chuàng)建完成后將當(dāng)前的實例存入單例池中。
}
}
到此,DAO類型的屬性就注入成功了,接下是注入CommonDAO。注入CommonDAO由于容器中沒有對應(yīng)的beanName,所有進(jìn)入上面的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, 當(dāng)前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的元數(shù)據(jù)了)
for (String beanName : this.beanDefinitionNames) {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName) ;
// 關(guān)鍵代碼
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;
}
}
}
}
到這你應(yīng)該清楚了為什么同時有DAO和CommonDAO注入時能成功了。但是當(dāng)沒有DAO注入的時候為什么就錯誤呢?原因其實在上面已經(jīng)給出了,你只要包裝我在注入CommonDAO時,容器中已經(jīng)將DAO這個bean實例創(chuàng)建存入到單例池中即可。這也就是為什么上面我們調(diào)整合理的順序后就能注入成功。還有就是你可以將CommonDAO的字段名稱改成與DAO bean的beanName一致也是可以的。
以上是本篇文章的全部內(nèi)容,希望對你有幫助。
完畢?。?!