@Autowired 发现不明确的依赖关系并且仍然有效。如何?

@Autowired finding ambiguous dependencies and still works. How?

为什么 Spring 不抛出 NoSuchBeanDefinitionException 存在不明确的依赖关系和使用 @Autowired 注释找到多个候选 bean 的地方?

我有一个简单的 beans.xml,它有两个相同的 bean,但 ID 不同 categorycategory1,出于某种原因 Spring 选择 category bean 进行自动装配。我的印象是 @Autowired 注释在内部使用 byType 自动装配 因为在这里找到了不止一个匹配 Spring 将抛出 NoSuchBeanDefinitionException 异常。

我在这里使用 spring 版本 3.2.13.RELEASE

beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans 
     .....   ">

        <context:annotation-config />

        <bean id="product" class="com.study.spring.Product">
            <property name="id" value="101"/>
            <property name="name" value="Apple iPhone"/>
            <property name="active" value="true"/>
        </bean>

        <bean id="category1" class="com.study.spring.Category">
            <property name="id" value="202"/>
            <property name="name" value="Phone"/>
            <property name="active" value="true"/>
        </bean>

        <bean id="category" class="com.study.spring.Category">
            <property name="id" value="201"/>
            <property name="name" value="Communications"/>
            <property name="active" value="true"/>
        </bean>

    </beans>

Product.java

package com.study.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class Product {
    private int id;
    private String name;
    private boolean active;
    @Autowired
    private Category category;

    //getters and setters here
}

它采用 ID 为 categoryCategory 对象,因为它与字段名称相匹配。旧 spring documentation 解释为:

"For a fallback match, the bean name is considered as a default qualifier value."

current documentation 对此解释得更清楚一些。这里有一个 "byName" 自动装配情况:

Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property.