springboot动态获取接口的实现类

  •           在springboot 的项目中,如果一个接口有多个实现类,我们需要动态获取,直接上代码;
  • springboot动态获取接口的实现类

 

@RestController
public class PersonController {

    @Resource(name="${active}")
    private HelloService helloService;

    @RequestMapping("/test")
    public String get() {
        return helloService.getName();
    }

}
public interface HelloService {

    String getName();

}



@Service("hello1")
public class HelloServiceImpl1  implements HelloService {
    @Override
    public String getName() {
        return "111";
    }
}



@Service("hello2")
public class HelloServiceImpl2 implements HelloService {

    @Override
    public String getName() {
        return "222";
    }
}

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }
}