springboot存在多个实现类如何进行选择
1:在springboot中,如果存在多个实现类实现同一个接口,可以使用@Qualifier,配合@Autowired进行选择,`
例如存在softCache和WeakCache两个实现类;
@Autowired
@Qualifier(“softCache”)
private Cache cache;
@Test
void TestCache() {
System.out.println(cache);
2:
也可以通过对象名直接选择
@Autowired//规则,先按照类型去查找注入,假如类型有多个在基于名字进行注入再按照名字进行注入;
//@Qualifier(“softCache”)//配合@Autowired使用用于指定对象
private Cache softCache;
@Test
void TestCache() {
System.out.println(softCache);