public interface TestService { String test(); } @Service("aTestService") public class ATestServiceImpl implements TestService{ @Override public String test() { return "A"; } } @Service("bTestService") public class BTestServiceImpl implements TestService { @Override public String test() { return "B"; } }
测试
@RestController @RequestMapping("/test") public class TestController { @Autowired TestServiceImpl testServiceImpl; @RequestMapping("/a")// localhost:8080//test/a?type=0 public String test(int type){ return testServiceImpl.test(type); } }
@Service public class TestServiceImpl { @Autowired @Qualifier("aTestService") TestService aTestService; @Autowired @Qualifier("bTestService") TestService bTestService; public String test(int type){ if (0==type) { return aTestService.test(); }else{ return bTestService.test(); } } }
结果:
http://localhost:8080//test/a?type=0 结果为A http://localhost:8080//test/a?type=1 结果为B