1.每个service的impl都可以指定名称(使用@Service(“名称”))
2.Controller中注入service的时候使用名称来指定注入哪一个。
(1).
@Autowired @Qualifier("名称")
(2).
@Resource(name="名称")
代码如下:
接口
public interface HumanService { public String name();}
接口实现类
@Service("teacherService")public class TeacherServiceImpl implements HumanService { @Override public String name() { System.out.println("teacher"); return "teacher"; }}
@Service("doctorService")public class DoctorServiceImpl implements HumanService { @Override public String name() { System.out.println("doctor"); return "doctor"; }}
控制器
@RestControllerpublic class HumanController {// @Resource(name="doctorService")
@Autowired @Qualifier("teacherService") private HumanService humanService; @RequestMapping("/name") public String name(){ return humanService.name(); }}
原文地址:https://www.cnblogs.com/zoe-java/p/11530888.html
时间: 2024-11-11 06:00:04