SpringBoot之分层解耦以及 IOC&DI的详细解析

本文介绍了软件开发中的内聚和耦合概念,阐述了高内聚低耦合原则,并通过实例展示了如何在Spring框架中使用控制反转(IOC)和依赖注入(DI)来解耦Controller、Service和Dao层的代码,提高代码的可重用性和移植性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

### 3.2 分层解耦

刚才我们学习过程序分层思想了,接下来呢,我们来学习下程序的解耦思想。

解耦:解除耦合。

#### 3.2.1 耦合问题

首先需要了解软件开发涉及到的两个概念:内聚和耦合。

- 内聚:软件中各个功能模块内部的功能联系。

- 耦合:衡量软件中各个层/模块之间的依赖、关联的程度。

**软件设计原则:高内聚低耦合。**

> 高内聚指的是:一个模块中各个元素之间的联系的紧密程度,如果各个元素(语句、程序段)之间的联系程度越高,则内聚性越高,即 "高内聚"。
>
> 低耦合指的是:软件中各个层、模块之间的依赖关联程序越低越好。

程序中高内聚的体现:

- EmpServiceA类中只编写了和员工相关的逻辑处理代码

程序中耦合代码的体现:

- 把业务类变为EmpServiceB时,需要修改controller层中的代码

高内聚、低耦合的目的是使程序模块的可重用性、移植性大大增强。

#### 3.2.2  解耦思路

之前我们在编写代码时,需要什么对象,就直接new一个就可以了。 这种做法呢,层与层之间代码就耦合了,当service层的实现变了之后, 我们还需要修改controller层的代码。

 那应该怎么解耦呢?

- 首先不能在EmpController中使用new对象。代码如下:

- 此时,就存在另一个问题了,不能new,就意味着没有业务层对象(程序运行就报错),怎么办呢?
  - 我们的解决思路是:
    - 提供一个容器,容器中存储一些对象(例:EmpService对象)
    - controller程序从容器中获取EmpService类型的对象

我们想要实现上述解耦操作,就涉及到Spring中的两个核心概念:

- **控制反转:** Inversion Of Control,简称IOC。对象的创建控制权由程序自身转移到外部(容器),这种思想称为控制反转。

  > 对象的创建权由程序员主动创建转移到容器(由容器创建、管理对象)。这个容器称为:IOC容器或Spring容器

- **依赖注入:** Dependency Injection,简称DI。容器为应用程序提供运行时,所依赖的资源,称之为依赖注入。

  > 程序运行时需要某个资源,此时容器就为其提供这个资源。
  >
  > 例:EmpController程序运行时需要EmpService对象,Spring容器就为其提供并注入EmpService对象

IOC容器中创建、管理的对象,称之为:bean对象

3.3 IOC&DI

上面我们引出了Spring中IOC和DI的基本概念,下面我们就来具体学习下IOC和DI的代码实现。

3.3.1 IOC&DI入门

任务:完成Controller层、Service层、Dao层的代码解耦

  • 思路:

    1. 删除Controller层、Service层中new对象的代码

    2. Service层及Dao层的实现类,交给IOC容器管理

    3. 为Controller及Service注入运行时依赖的对象

      • Controller程序中注入依赖的Service层对象

      • Service程序中注入依赖的Dao层对象

第1步:删除Controller层、Service层中new对象的代码

第2步:Service层及Dao层的实现类,交给IOC容器管理

  • 使用Spring提供的注解:@Component ,就可以实现类交给IOC容器管理

第3步:为Controller及Service注入运行时依赖的对象

  • 使用Spring提供的注解:@Autowired ,就可以实现程序运行时IOC容器自动注入需要的依赖对象

完整的三层代码:

  • Controller层:

@RestController
public class EmpController {
​
    @Autowired //运行时,从IOC容器中获取该类型对象,赋值给该变量
    private EmpService empService ;
​
    @RequestMapping("/listEmp")
    public Result list(){
        //1. 调用service, 获取数据
        List<Emp> empList = empService.listEmp();
​
        //3. 响应数据
        return Result.success(empList);
    }
}
  • Service层:

@Component //将当前对象交给IOC容器管理,成为IOC容器的bean
public class EmpServiceA implements EmpService {
​
    @Autowired //运行时,从IOC容器中获取该类型对象,赋值给该变量
    private EmpDao empDao ;
​
    @Override
    public List<Emp> listEmp() {
        //1. 调用dao, 获取数据
        List<Emp> empList = empDao.listEmp();
​
        //2. 对数据进行转换处理 - gender, job
        empList.stream().forEach(emp -> {
            //处理 gender 1: 男, 2: 女
            String gender = emp.getGender();
            if("1".equals(gender)){
                emp.setGender("男");
            }else if("2".equals(gender)){
                emp.setGender("女");
            }
​
            //处理job - 1: 讲师, 2: 班主任 , 3: 就业指导
            String job = emp.getJob();
            if("1".equals(job)){
                emp.setJob("讲师");
            }else if("2".equals(job)){
                emp.setJob("班主任");
            }else if("3".equals(job)){
                emp.setJob("就业指导");
            }
        });
        return empList;
    }
}

Dao层:

@Component //将当前对象交给IOC容器管理,成为IOC容器的bean
public class EmpDaoA implements EmpDao {
    @Override
    public List<Emp> listEmp() {
        //1. 加载并解析emp.xml
        String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
        System.out.println(file);
        List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
        return empList;
    }
}

运行测试:

分层解耦、IOC(控制反转)和DI(依赖注入)是现代Java应用程序开发中非常重要的设计模式和技术。它们帮助开发者构建更灵活、可维护和可测试的系统。 ### 分层解耦 分层解耦是一种软件设计原则,它将应用程序分为多个层次(如表现层、业务逻辑层、数据访问层等),每一层只负责特定的功能,并且层与层之间通过接口进行交互,从而降低各层之间的耦合度。 #### 示例代码:分层架构 ```java // 数据访问层接口 public interface UserRepository { User findUserById(int id); } // 数据访问层实现 public class UserRepositoryImpl implements UserRepository { @Override public User findUserById(int id) { // 模拟数据库查询 return new User(id, "John Doe"); } } // 业务逻辑层 public class UserService { private UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public User getUserById(int id) { return userRepository.findUserById(id); } } // 表现层 public class UserController { private UserService userService; public UserController(UserService userService) { this.userService = userService; } public void displayUser(int id) { User user = userService.getUserById(id); System.out.println("User: " + user.getName()); } } // 测试类 public class MainApp { public static void main(String[] args) { // 创建对象并模拟分层调用 UserRepository userRepository = new UserRepositoryImpl(); UserService userService = new UserService(userRepository); UserController userController = new UserController(userService); userController.displayUser(1); } } ``` ### IOC(控制反转) IOC是一种设计模式,它的核心思想是将对象的创建和管理交给容器,而不是由程序自己手动创建和管理对象。这样可以减少对象之间的耦合度,提高代码的可测试性和可维护性。 #### 示例代码:使用Spring框架的IOC容器 ```java import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; // 配置类 @Configuration public class AppConfig { @Bean public UserRepository userRepository() { return new UserRepositoryImpl(); } @Bean public UserService userService() { return new UserService(userRepository()); } @Bean public UserController userController() { return new UserController(userService()); } } public class SpringMainApp { public static void main(String[] args) { // 使用Spring的IOC容器 ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserController userController = context.getBean(UserController.class); userController.displayUser(1); } } ``` ### DI(依赖注入) DI是IOC的一种具体实现方式,它通过外部配置或注解将依赖关系注入到目标对象中,而不是让对象自己创建或查找依赖。 #### 示例代码:使用Spring框架的DI ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class UserServiceWithDI { private final UserRepository userRepository; @Autowired public UserServiceWithDI(UserRepository userRepository) { this.userRepository = userRepository; } public User getUserById(int id) { return userRepository.findUserById(id); } } @Component public class UserControllerWithDI { private final UserServiceWithDI userService; @Autowired public UserControllerWithDI(UserServiceWithDI userService) { this.userService = userService; } public void displayUser(int id) { User user = userService.getUserById(id); System.out.println("User: " + user.getName()); } } public class SpringDiMainApp { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserControllerWithDI userController = context.getBean(UserControllerWithDI.class); userController.displayUser(1); } } ``` ### 解释 1. **分层解耦**:通过将应用程序分为不同的层次(如表现层、业务逻辑层、数据访问层),每一层只负责特定的功能,并通过接口进行交互,从而降低各层之间的耦合度。 2. **IOC(控制反转)**:将对象的创建和管理交给容器,而不是由程序自己手动创建和管理对象。这样可以减少对象之间的耦合度,提高代码的可测试性和可维护性。 3. **DI(依赖注入)**:通过外部配置或注解将依赖关系注入到目标对象中,而不是让对象自己创建或查找依赖。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向着五星的方向

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值