简体   繁体   中英

Spring (autowired) component use multiple in the same class

I have been started use Spring recently. 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..."

This is because ambiguity and spring is confused which one to consider so please tell spring which one to use by qualifying like below :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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