• 使用 Setter 方法实现 Spring 依赖注入


    Spring 框架中,依赖注入(Dependency Injection, DI)是一种重要的设计模式,它允许我们通过容器管理对象的创建及其依赖关系。除了构造函数注入之外,Spring 还支持通过 Setter 方法注入依赖。本文将详细介绍如何使用 Setter 方法注入实现 Spring 依赖注入。

    一、什么是依赖注入

    依赖注入是一种设计模式,它将对象的依赖关系交给外部容器管理,而不是在对象内部自行创建这些依赖。Spring 框架通过 IoC(控制反转)容器实现了依赖注入,允许开发者轻松地管理对象及其依赖关系。

    二、Setter 方法注入概述

    Setter 方法注入是通过对象的 Setter 方法来注入依赖。这种方式的特点是可以在对象创建后再设置其依赖,因此比较灵活,可以在对象生命周期中动态更改依赖。

    优点

    • 灵活性:可以在对象创建之后再设置依赖。
    • 可选依赖:适合用于可选依赖的场景。

    缺点

    • 延迟初始化:依赖注入可能会在对象创建之后进行,容易导致依赖未及时注入的问题。
    • 依赖散布:依赖关系分散在多个 Setter 方法中,可能导致代码的可读性下降。

    三、使用 Setter 方法注入实现依赖注入

    1. 项目结构

    一个典型的 Spring 项目结构如下:

    my-spring-setter-injection-project/
    ├── src/
    │   ├── main/
    │   │   ├── java/
    │   │   │   └── com/
    │   │   │       └── example/
    │   │   │           └── project/
    │   │   │               ├── config/
    │   │   │               │   └── AppConfig.java
    │   │   │               ├── service/
    │   │   │               │   ├── ExampleService.java
    │   │   │               │   └── AnotherService.java
    │   │   │               └── controller/
    │   │   │                   └── ExampleController.java
    │   └── resources/
    │       └── application.properties
    └── pom.xml
    

    2. Java 类定义

    // ExampleService.java
    package com.example.project.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class ExampleService {
        public String serve() {
            return "Service is serving";
        }
    }
    
    // AnotherService.java
    package com.example.project.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class AnotherService {
        public String assist() {
            return "Another service is assisting";
        }
    }
    
    // ExampleController.java
    package com.example.project.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import com.example.project.service.ExampleService;
    import com.example.project.service.AnotherService;
    
    @Controller
    public class ExampleController {
        private ExampleService exampleService;
        private AnotherService anotherService;
    
        @Autowired
        public void setExampleService(ExampleService exampleService) {
            this.exampleService = exampleService;
        }
    
        @Autowired
        public void setAnotherService(AnotherService anotherService) {
            this.anotherService = anotherService;
        }
    
        public void doSomething() {
            System.out.println(exampleService.serve());
            System.out.println(anotherService.assist());
        }
    }
    

    3. 配置类

    // AppConfig.java
    package com.example.project.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(basePackages = "com.example.project")
    public class AppConfig {
    }
    

    4. 启动类

    Spring Boot 项目通常包含一个启动类,用于启动 Spring 应用。

    // Application.java
    package com.example.project;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    5. application.properties

    配置文件用于配置应用程序参数,如服务器端口、数据库连接等。

    server.port=8080
    

    6. 测试类

    // ApplicationTests.java
    package com.example.project;
    
    import com.example.project.controller.ExampleController;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    public class ApplicationTests {
        
        @Autowired
        private ExampleController exampleController;
    
        @Test
        public void testDoSomething() {
            exampleController.doSomething();
        }
    }
    

    四、总结

    Setter 方法注入是实现 Spring 依赖注入的一种灵活方式。它通过对象的 Setter 方法来设置依赖,适合用于需要在对象创建后动态更改依赖的场景。虽然 Setter 方法注入具有灵活性,但也可能导致依赖延迟初始化的问题。在实际开发中,应根据具体需求选择合适的依赖注入方式。

  • 相关阅读:
    Linux——多线程(三)
    【安装windows10 RTX3060 tensorflow的开发环境】
    解决yolo无法指定显卡的问题,实测v5、v7、v8有效
    sqlsession对象为什么不能被共享?
    图像基础知识、深度学习基础知识以及相关问题
    数据结构—内部排序(下)
    DNS与网站访问流程
    axios添加缓存请求,防止多次请求,单页面多个同一组件造成多次请求解决方案
    最全前端面试总结(2)map/reduce
    【从零开始学微服务】05.微服务的优势和不足
  • 原文地址:https://blog.csdn.net/FireFox1997/article/details/139823490