Spring-IOC-DI(控制反转&依赖注入入门操作)
This commit is contained in:
@@ -2,7 +2,9 @@ package com.inmind;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
//@ComponentScan({"dao","com.inmind"}) ,spring的4大组件注解必须要被扫描到才能生效,默认扫描启动项同一级类或子包中的类
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class SpringbootWebReqRespApplication {
|
public class SpringbootWebReqRespApplication {
|
||||||
|
|
||||||
|
@@ -5,15 +5,26 @@ import com.inmind.pojo.Result;
|
|||||||
import com.inmind.service.EmpService;
|
import com.inmind.service.EmpService;
|
||||||
import com.inmind.service.impl.EmpServiceA;
|
import com.inmind.service.impl.EmpServiceA;
|
||||||
import com.inmind.utils.XmlParserUtils;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class EmpController {
|
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")
|
/*@RequestMapping("/listEmp")
|
||||||
public Result listEmp(){
|
public Result listEmp(){
|
||||||
|
@@ -3,9 +3,12 @@ package com.inmind.dao.impl;
|
|||||||
import com.inmind.dao.EmpDao;
|
import com.inmind.dao.EmpDao;
|
||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import com.inmind.utils.XmlParserUtils;
|
import com.inmind.utils.XmlParserUtils;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
//从XML文件中获取数据
|
//从XML文件中获取数据
|
||||||
|
@Component//交给IOC容器管理
|
||||||
|
//@Repository
|
||||||
public class EmpDaoA implements EmpDao {
|
public class EmpDaoA implements EmpDao {
|
||||||
@Override
|
@Override
|
||||||
public List<Emp> listEmp() {
|
public List<Emp> listEmp() {
|
||||||
|
@@ -1,15 +1,22 @@
|
|||||||
package com.inmind.service.impl;
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
import com.inmind.dao.EmpDao;
|
import com.inmind.dao.EmpDao;
|
||||||
import com.inmind.dao.impl.EmpDaoA;
|
|
||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import com.inmind.service.EmpService;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
|
//@Component//将当前类交给IOC容器管理,称为IOC容器中的bean对象(IOC控制反转)
|
||||||
|
//@Primary
|
||||||
|
@Service
|
||||||
public class EmpServiceA implements EmpService {
|
public class EmpServiceA implements EmpService {
|
||||||
|
|
||||||
private EmpDao empDao = new EmpDaoA();
|
@Autowired //IOC容器进行依赖注入赋值
|
||||||
|
private EmpDao empDao;
|
||||||
/*
|
/*
|
||||||
获取到数据之后,对员工数据加以处理
|
获取到数据之后,对员工数据加以处理
|
||||||
*/
|
*/
|
||||||
|
@@ -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<Emp> listEmp() {
|
||||||
|
//1.从dao层获取数据
|
||||||
|
List<Emp> 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;
|
||||||
|
}
|
||||||
|
}
|
@@ -3,6 +3,8 @@ package com.inmind.utils;
|
|||||||
import org.dom4j.Document;
|
import org.dom4j.Document;
|
||||||
import org.dom4j.Element;
|
import org.dom4j.Element;
|
||||||
import org.dom4j.io.SAXReader;
|
import org.dom4j.io.SAXReader;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
Reference in New Issue
Block a user