简体   繁体   English

Spring(autowired)组件在同一类中使用多个

[英]Spring (autowired) component use multiple in the same class

I have been started use Spring recently. 我最近已经开始使用Spring。 And I didn't find the solution to my problem. 而且我没有找到解决问题的方法。

I have a component: 我有一个组成部分:

@Component
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Log4j2
public class CompX { ....

I would like to use it twice in a (component) class: 我想在(组件)类中使用两次:

...
@Autowired
private CompX current;

@Autowired
private CompX old;
...

How can I do this? 我怎样才能做到这一点? Always got following exception. 总是有以下异常。

"org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type..." “ org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有符合条件的bean类型...”

This is because ambiguity and spring is confused which one to consider so please tell spring which one to use by qualifying like below : 这是因为歧义性和spring混淆了要考虑的那个,所以请通过以下限定告诉spring要使用哪个:

@Autowired
@Qualifier("current")
private CompX current;

@Autowired
@Qualifier("old")
private CompX old;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM