在module1.jar:具有多个自动装配类型的依赖关系解决方案?
<bean id="customerService" class="com.service.CustomCustomerServiceImpl" > </bean>
在module2.jar:
<bean id="customerService" class="com.service.CustomerServiceImpl" > </bean>
public class CustomerController {
@Autowired
protected CustomerService customerService;
// getters and setters
}
我看到的CustomerService与CustomCustomerServiceImpl总是注射。
我的问题是,在启动服务器 时应该抛出异常,因为有两个类型为CustomerService的Bean(CustomCustomerServiceImpl和CustomerServiceImpl)。
不应该弹出异常?
当多个bean被发现时,spring如何通过type来解析autowire注解?
更新: -
public class CustomCustomerServiceImpl extends CustomerServiceImpl {}
由于您为每个bean设置了一个'id',因此Spring会尝试将bean'id'与您要注入的属性的名称进行匹配。因此,您将始终注入'customerService',因为这是您的类属性的名称。如果你没有为bean设置'id',那么是的,你可能会得到一个异常。 – icabod
@icabod两个id都是一样的。所以'CustomCustomerServiceImpl'如何被注入而不是错误? – user3198603
几分钟前,这些id不一样,其中一个bean有另一个作为父项,意味着它可以覆盖基本bean。 – icabod