@Component
public interface Aorder {
void say();
}
@Component
public class Corder implements Aorder{
@Override
public void say() {
System.out.println("----------Corder------------");
}
}
@Component
public class Dorder implements Aorder{
@Override
public void say() {
System.out.println("----------Border------------");
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class OrderTest {
@Autowired
private MyOrderTest myOrderTest;
@Test
public void say(){
myOrderTest.test();
}
}
此时会报错,调用Aorder时不知道调用哪一个bean
@Resource
@Service
public class MyOrderTest {
@Resource(type = Corder.class)
private Aorder aorder;
public void test(){
aorder.say();
}
}
此时就能正常调用了,但是Corder,Dorder都实现Aorder,此时Corder,Dorder 都生成了bean
@Primary
@Component
@Primary
public class Corder implements Aorder{
@Override
public void say() {
System.out.println("----------Corder------------");
}
}
Corder,Dorder都实现Aorder,此时只有Corder会生成bean,Dorder不会生成bean