From bf40a0d81550d40ed096e8379fe7941718c6ff11 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sun, 17 May 2026 16:30:04 +0800 Subject: [PATCH] =?UTF-8?q?javaEEday05-=E5=88=86=E5=B1=82=E8=A7=A3?= =?UTF-8?q?=E8=80=A6-IOC&DI-DI=E8=AF=A6=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/inmind/controller/EmpController.java | 4 ++++ .../src/main/java/com/inmind/service/impl/EmpServiceA.java | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) 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