diff --git a/springboot-web-req-resp/src/main/java/com/inmind/controller/EmpController.java b/springboot-web-req-resp/src/main/java/com/inmind/controller/EmpController.java index cc7fbab..0d96282 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/controller/EmpController.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/controller/EmpController.java @@ -5,7 +5,9 @@ import com.inmind.pojo.Result; import com.inmind.service.EmpService; import com.inmind.service.impl.EmpServiceA; import com.inmind.utils.XmlParserUtils; +import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,6 +20,8 @@ public class EmpController { //定义业务层对象 @Autowired //运行时,IOC容器会提供该类型的bean对象,并赋值给该变量---依赖注入 + @Qualifier("empServiceB") //方式二:解决同一类型,有多个bean对象 + //@Resource(name = "empServiceA") 方式三:解决同一类型,有多个bean对象 private EmpService empService;//多态 //定义一个获取员工列表的接口 diff --git a/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceA.java b/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceA.java index c11bb21..75b60b5 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceA.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceA.java @@ -4,9 +4,12 @@ import com.inmind.dao.EmpDao; import com.inmind.pojo.Emp; import com.inmind.service.EmpService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; import java.util.List; - +//@Primary 方式一:解决同一类型,有多个bean对象 +@Service public class EmpServiceA implements EmpService { //获取dao层的对象,获取数据 @Autowired