网站广告销售怎么做,这2个代码 找做网站的 安装一下,苏州新海通网站建设,如何改变wordpress字体颜色一.依赖注入的注解
在我们的项目中#xff0c;EmpService的实现类有两个#xff0c;分别是EmpServiceA和EmpServiceB。这两个实现类都加上Service注解。我们运行程序#xff0c;就会报错。 这是因为我们依赖注入的注解Autowired默认是按照类型来寻找bean对象的进行依赖注入…一.依赖注入的注解
在我们的项目中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 ListEmp listEmp() {ListEmp 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(){ListEmp 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);ListEmp 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(){ListEmp 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);ListEmp 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注解。 五.总结