public interface Svc { void sayHello(); } @Service public class SvcA implements Svc { @Override public void sayHello() { System.out.println("hello, this is service A"); } } @Service public class SvcB implements Svc { @Override public void sayHello() { System.out.println("hello, this is service B"); } } @Service public class SvcC implements Svc { @Override public void sayHello() { System.out.println("hello, this is service C"); } }
@SpringBootTest public class SimpleTest { @Autowired // @Qualifier("svcA") Svc svc; @Test void rc() { Assertions.assertNotNull(svc); svc.sayHello(); } }
@Service public class HelpService { @Autowired @Qualifier("svcB") private Svc svc; public void sayHello() { svc.sayHello(); } } public interface Svc { void sayHello(); } @Service public class SvcB implements Svc { @Override public void sayHello() { System.out.println("hello, this is service B"); } }
@Service public class HelpService { private final Svc svc; @Autowired public HelpService(@Qualifier("svcB") Svc svc) { // Assert.notNull(svc, "svc must not be null"); this.svc = svc; } public void sayHello() { svc.sayHello(); } }
@Autowired private Svc svc;
private Helper helper; @Autowired public void setHelper(Helper helper) { this.helper = helper; }
private final Svc svc; @Autowired public HelpService(@Qualifier("svcB") Svc svc) { this.svc = svc; }