一个很有意思的Spring注入问题,你遇到过吗?

开发 前端
在AppConfig配置类中定义了DAO bean实例,在CommonService中分别去注入DAO 接口和CommonDAO。运行上面的程序没有问题正常。

环境: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。运行上面的程序没有问题正常。

2. 问题汇总

2.1 修改注入1

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但是容器中没有,是不是很奇怪。

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的注册方式,运行程序还是错误

2.5 修改注入5

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

根据2.4的情况,修改注册DAO与CommonService的顺序后,程序又正确了。

3. 原因解析

当如下方式注入时

@Resource
private DAO dao ;
@Resource
private 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一致也是可以的。

以上是本篇文章的全部内容,希望对你有帮助。

完毕!!!

责任编辑:武晓燕 来源: Spring全家桶实战案例源码
相关推荐

2015-03-12 10:46:30

代码代码犯罪

2020-12-12 13:50:16

云开发

2021-01-27 13:54:05

开发云原生工具

2018-06-24 16:39:28

Tomcat异常线程

2011-04-26 09:22:05

SQLite

2021-10-28 19:35:02

代码main方法

2020-11-08 14:38:35

JavaScript代码开发

2010-04-09 11:24:59

Oracle 排序

2023-05-15 09:16:18

CSSCSS Mask

2009-07-23 15:07:32

2018-04-25 10:57:00

AIX报错vios

2020-10-12 09:49:14

C++ 开发代码

2023-03-13 07:41:34

分页查询数据排序

2021-12-26 14:32:11

缓存数据库数据

2009-08-26 17:53:31

C# DropDown

2022-03-21 10:21:50

jQuery代码模式

2015-01-05 10:13:37

2021-03-25 06:12:55

SVG 滤镜CSS

2020-09-24 10:49:09

iOSiPadOSBug

2021-02-19 11:01:46

异步竞态接口异步
点赞
收藏

51CTO技术栈公众号