From 7228061094c894f43cc39e3f4f66675ea2a3c6b9 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 18 Sep 2025 16:31:39 +0800 Subject: [PATCH] =?UTF-8?q?Spring-IOC-DI(=E6=8E=A7=E5=88=B6=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC&=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5=E5=85=A5?= =?UTF-8?q?=E9=97=A8=E6=93=8D=E4=BD=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringbootWebReqRespApplication.java | 2 + .../com/inmind/controller/EmpController.java | 13 +++++- .../java/com/inmind/dao/impl/EmpDaoA.java | 3 ++ .../com/inmind/service/impl/EmpServiceA.java | 11 ++++- .../com/inmind/service/impl/EmpServiceB.java | 46 +++++++++++++++++++ .../java/com/inmind/utils/XmlParserUtils.java | 2 + 6 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceB.java diff --git a/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java b/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java index 08b0910..28f29c5 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java @@ -2,7 +2,9 @@ package com.inmind; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +//@ComponentScan({"dao","com.inmind"}) ,spring的4大组件注解必须要被扫描到才能生效,默认扫描启动项同一级类或子包中的类 @SpringBootApplication public class SpringbootWebReqRespApplication { 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 7f070d0..4d6aa9d 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,15 +5,26 @@ import com.inmind.pojo.Result; import com.inmind.service.EmpService; import com.inmind.service.impl.EmpServiceA; import com.inmind.utils.XmlParserUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.annotation.Resource; import java.util.List; @RestController public class EmpController { - private EmpService empService = new EmpServiceA(); +/* @Autowired//运行时,IOC容器会提高该类型的bean对象,并赋值(依赖注入) +// @Qualifier("empServiceA")//注入时直接指定Bean对象的名称 + @Qualifier("empServiceB")//注入时直接指定Bean对象的名称 + private EmpService empService;*/ + + //方式三 + @Resource(name = "empServiceB") + private EmpService empService; /*@RequestMapping("/listEmp") public Result listEmp(){ diff --git a/springboot-web-req-resp/src/main/java/com/inmind/dao/impl/EmpDaoA.java b/springboot-web-req-resp/src/main/java/com/inmind/dao/impl/EmpDaoA.java index 49b43d6..9e6ad5c 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/dao/impl/EmpDaoA.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/dao/impl/EmpDaoA.java @@ -3,9 +3,12 @@ package com.inmind.dao.impl; import com.inmind.dao.EmpDao; import com.inmind.pojo.Emp; import com.inmind.utils.XmlParserUtils; +import org.springframework.stereotype.Component; import java.util.List; //从XML文件中获取数据 +@Component//交给IOC容器管理 +//@Repository public class EmpDaoA implements EmpDao { @Override public List listEmp() { 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 d4f9fee..9735df9 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 @@ -1,15 +1,22 @@ package com.inmind.service.impl; import com.inmind.dao.EmpDao; -import com.inmind.dao.impl.EmpDaoA; 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.Component; +import org.springframework.stereotype.Service; import java.util.List; +//@Component//将当前类交给IOC容器管理,称为IOC容器中的bean对象(IOC控制反转) +//@Primary +@Service public class EmpServiceA implements EmpService { - private EmpDao empDao = new EmpDaoA(); + @Autowired //IOC容器进行依赖注入赋值 + private EmpDao empDao; /* 获取到数据之后,对员工数据加以处理 */ diff --git a/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceB.java b/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceB.java new file mode 100644 index 0000000..fecce56 --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/service/impl/EmpServiceB.java @@ -0,0 +1,46 @@ +package com.inmind.service.impl; + +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; + +//@Component//将当前类交给IOC容器管理,称为IOC容器中的bean对象(IOC控制反转) +//@Primary +@Service +public class EmpServiceB implements EmpService { + + @Autowired //IOC容器进行依赖注入赋值 + private EmpDao empDao; + /* + 获取到数据之后,对员工数据加以处理 + */ + @Override + public List listEmp() { + //1.从dao层获取数据 + List empList = empDao.listEmp(); + //2.对数据进行转换 + empList.stream().forEach(emp->{ + //处理性别 gender: 1就是男 2就是女 + if (emp.getGender().equals("1")) { + emp.setGender("男士"); + } else { + emp.setGender("女士"); + } + //处理职位 job : 1 - 讲师 2-班主任 3-辅导员 + String job = emp.getJob(); + if ("1".equals( job)) { + emp.setJob("讲师1"); + } else if ("2".equals(job)) { + emp.setJob("班主任1"); + } else { + emp.setJob("辅导员"); + } + }); + return empList; + } +} diff --git a/springboot-web-req-resp/src/main/java/com/inmind/utils/XmlParserUtils.java b/springboot-web-req-resp/src/main/java/com/inmind/utils/XmlParserUtils.java index 298988e..9cc2659 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/utils/XmlParserUtils.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/utils/XmlParserUtils.java @@ -3,6 +3,8 @@ package com.inmind.utils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; +import org.springframework.stereotype.Component; + import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field;