分层解耦-03.IOC&DI-入门

一. IOC&DI入门

二.控制转移注解@Component

因为dao和service接口的实现类对象需要传入到service和controller中,因此需要将dao和service代码加上@Component注解,使之实现控制反转,将实现类对象交给IOC容器管理,成为IOC容器中的bean。

java 复制代码
package com.gjw.dao.impl;

import com.gjw.dao.EmpDao;
import com.gjw.pojo.Emp;
import com.gjw.utils.XmlParserUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component  // IOC:控制反转,将实现类对象交给容器。将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpDaoA implements EmpDao {
    @Override
    public List<Emp> listEmp() {
        String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
        System.out.println(file);
        List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
        return empList;
    }
}
java 复制代码
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.stereotype.Component;

import java.util.List;

@Component  // IOC:控制反转,将实现类对象交给容器。将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpServiceB implements EmpService {
    @Autowired  // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量
    private EmpDao empDao;
    @Override
    public 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;
    }
}

三.依赖注入注解@Autowired

因为service和controller分别依靠dao和service接口的实现类对象,因此需要在其依赖对象上加上@Autowired注解来注入依赖。

java 复制代码
package com.gjw.controller;
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.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对象,并赋值给该变量
    private EmpService empService;
    @RequestMapping("/listEmp")
    public Result list(){

        List<Emp> empList = empService.listEmp();
        return Result.success(empList);
    }
}
java 复制代码
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.stereotype.Component;

import java.util.List;

@Component  // IOC:控制反转,将实现类对象交给容器。将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpServiceB implements EmpService {
    @Autowired  // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量
    private EmpDao empDao;
    @Override
    public 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;
    }
}

四.总结

注意:如果有新的实现类,比如EmpServiceA,那么要使用新的实现类只需要在上面添加@Component注解,并将原先的实现类的@Component注解注释掉即可。这样就会将EmpServiceA的bean对象交给IOC容器,而controller里面的代码不用动。

java 复制代码
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.stereotype.Component;

import java.util.List;

@Component  // IOC:控制反转,将实现类对象交给容器。将当前类交给IOC容器管理,成为IOC容器中的bean
public class EmpServiceA implements EmpService {
    @Autowired  // DI:依赖注入,service依赖于dao,运行时IOC容器会提供该类型的bean对象,并赋值给该变量
    private EmpDao empDao;
    @Override
    public 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;
    }
}
相关推荐
RuoyiOffice2 分钟前
SpringBoot3+OnlyOffice 自动保存版本:定时回存、间隔合并与 50 版上限怎么配
spring boot·在线文档·onlyoffice·版本管理·spring boot 3·自动保存·ruoyi office
huaweichenai9 分钟前
spring boot使用hutool实现调用外部接口
java·spring boot
峥嵘life17 分钟前
2026免费的 opencode 使用分享:Windows端 + 服务器CLI 实战总结
android·大数据·开发语言
纪念 22924 分钟前
C++类和对象(二)
开发语言·c++
泡海椒32 分钟前
Java 后端最优 PDF 导出方案:jquick-pdf 项目引入与快速测试
java·开发语言·pdf
Evand J33 分钟前
【MATLAB例程】二维Astar路径规划与AOA-TDOA定位仿真。完整代码,附下载链接。包运行成功,带中文注释
开发语言·matlab·路径规划·代码·定位·融合定位
李永奉35 分钟前
中科蓝讯SDK开发-耳机项目功耗问题排查教程
c语言·开发语言·嵌入式硬件·物联网·智能手机
阿飞76638 分钟前
订单超时自动关单:RabbitMQ 延迟队列 + 定时任务"双保险"实践
java
二炮手亮子44 分钟前
使用云服务+hermes+mino模型 我用微信操控服务器自己编程并部署实操
java·语言模型·ai编程
dkbnull1 小时前
Spring Boot配置文件敏感信息加密解密
spring boot·后端