当前位置: 首页 > news >正文

分层解耦-05.IOCDI-DI详解

一.依赖注入的注解

在我们的项目中,EmpService的实现类有两个,分别是EmpServiceA和EmpServiceB。这两个实现类都加上@Service注解。我们运行程序,就会报错。

这是因为我们依赖注入的注解@Autowired默认是按照类型来寻找bean对象的进行依赖注入的,controller程序会在IOC容器中寻找到两个service的bean对象,此时他会不知道使用哪一个,就会上面的报错。 

@Autowired      // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量private EmpService empService;

为了解决这个问题,要么就将其中的一个@Service注解去掉,或者采用方法二,使用依赖注入的注解来指定注解的优先级。

二.@Primary注解

@Primary注解作用在bean对象上,当IOC容器中有多个不同实现类的bean对象时,哪个实现类上面加上了@Primary注解,哪个实现类的bean对象就会起作用。

package com.gjw.service.impl;import com.gjw.dao.EmpDao;
import com.gjw.pojo.Emp;
import com.gjw.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容器管理,成为IOC容器中的bean
@Primary
@Service
public class EmpServiceA implements EmpService {@Autowired  // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量private EmpDao empDao;@Overridepublic List<Emp> listEmp() {List<Emp> empList = empDao.listEmp();empList.stream().forEach(emp ->{if ("1".equals(emp.getGender())) {emp.setGender("男");} else if ("2".equals(emp.getGender())) {emp.setGender("女");}if ("1".equals(emp.getJob())) {emp.setJob("讲师");} else if ("2".equals(emp.getJob())) {emp.setJob("班主任");} else if ("3".equals(emp.getJob())) {emp.setJob("就业指导");}});return empList;}
}

 

三.@Qualifier注解

@Qualifier注解主要是配合着@Autowired注解使用,在要注入的类(此处是controller)的@Autowired注解下面加上@Qualifier(bean对象名字)

(bean对象名字)默认是实现类类名首字母小写

package com.gjw.controller;
/*对xml文件进行处理,从而加载处理要响应的数据*/
import com.gjw.pojo.Emp;
import com.gjw.pojo.Result;
import com.gjw.service.EmpService;
import com.gjw.service.impl.EmpServiceA;
import com.gjw.utils.XmlParserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;@RestController
public class EmpController {@Autowired      // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量@Qualifier("empServiceB")private EmpService empService;@RequestMapping("/listEmp")public Result list(){List<Emp> empList = empService.listEmp();return Result.success(empList);}
/*@RequestMapping("/listEmp")public Result list(){// 1.加载emp.xml,并解析emp.xml中的数据String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();System.out.println(file);List<Emp> empList = XmlParserUtils.parse(file, Emp.class);// 2.对员工信息中的gender,job字段进行处理empList.stream().forEach(emp ->{if ("1".equals(emp.getGender())) {emp.setGender("男");} else if ("2".equals(emp.getGender())) {emp.setGender("女");}if ("1".equals(emp.getJob())) {emp.setJob("讲师");} else if ("2".equals(emp.getJob())) {emp.setJob("班主任");} else if ("3".equals(emp.getJob())) {emp.setJob("就业指导");}});// 3.组装数据并返回return Result.success(empList);}
*/
}

指定empServiceB这个bean对象生效。 

四.@Resouce注解 

使用@Resouce注解,@Autowired注解默认是按照bean对象的类型进行选择的。@Resouce注解是按照名称进行bean对象的选择的。@Resouce(bean对象名字)

package com.gjw.controller;
/*对xml文件进行处理,从而加载处理要响应的数据*/
import com.gjw.pojo.Emp;
import com.gjw.pojo.Result;
import com.gjw.service.EmpService;
import com.gjw.service.impl.EmpServiceA;
import com.gjw.utils.XmlParserUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;@RestController
public class EmpController {/*@Autowired      // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量@Qualifier("empServiceB")*/@Resource(name = "empServiceB")private EmpService empService;@RequestMapping("/listEmp")public Result list(){List<Emp> empList = empService.listEmp();return Result.success(empList);}
/*@RequestMapping("/listEmp")public Result list(){// 1.加载emp.xml,并解析emp.xml中的数据String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();System.out.println(file);List<Emp> empList = XmlParserUtils.parse(file, Emp.class);// 2.对员工信息中的gender,job字段进行处理empList.stream().forEach(emp ->{if ("1".equals(emp.getGender())) {emp.setGender("男");} else if ("2".equals(emp.getGender())) {emp.setGender("女");}if ("1".equals(emp.getJob())) {emp.setJob("讲师");} else if ("2".equals(emp.getJob())) {emp.setJob("班主任");} else if ("3".equals(emp.getJob())) {emp.setJob("就业指导");}});// 3.组装数据并返回return Result.success(empList);}
*/
}

注意:当我们使用Resouce注解时,是JDK框架提供Resouce注解。而使用Autowired时使用的是springboot框架提供Autowired注解。

 五.总结

 

http://www.lryc.cn/news/455086.html

相关文章:

  • HCIP-HarmonyOS Application Developer 习题(六)
  • 【电路基础 · 3】实际电压源 实际电流源;两种电源的等效情况;戴维南模型 诺顿模型(自用)
  • 案例-猜数字游戏
  • POI数据的处理与分析
  • ansible部分模块学习
  • 数据库(MySQL):使用命令从零开始在Navicat创建一个数据库及其数据表(二).设置主键自增等特点
  • SQL第13课——创建高级联结
  • 订阅ROS2中相机的相关话题并保存RGB、深度和点云图
  • Open WebUI | 自托管的类 ChatGPT 网站
  • 【Python】Python知识总结浅析
  • c#代码介绍23种设计模式_20策略者模式
  • FPGA-UART串口接收模块的理解
  • 复习HTML(基础)
  • Linux聊天集群开发之环境准备
  • can 总线入门———can简介硬件电路
  • 【重学 MySQL】六十、空间类型
  • python实现DES算法
  • 基于LORA的一主多从监测系统_框架搭建
  • 优化理论及应用精解【25】
  • 贝锐蒲公英网盘首发,秒建私有云,高速远程访问
  • [ 蓝桥 ·算法双周赛 ] 第 19 场 小白入门赛
  • HTML+CSS基础 第二季课堂笔记
  • 【Easy RL】Easy RL蘑菇书全书学习笔记
  • JavaWeb(二)
  • 【C++】--类和对象(2)
  • 最新BurpSuite2024.9专业中英文开箱即用版下载
  • C++ 观察者模式
  • 基于pytorch的手写数字识别-训练+使用
  • SpringBoot接收前端传递参数
  • 【LeetCode周赛】第 418 场