网站开发最严重的问题,网上超市系统,设计师接私活的网站,网站网站怎么做的Spring Boot 中的依赖注入#xff08;Dependency Injection#xff0c;简称 DI#xff09;是通过 Spring 框架的核心机制——控制反转#xff08;Inversion of Control#xff0c;IOC#xff09;容器来实现的。Spring Boot 基于 Spring Framework#xff0c;在应用中自动…Spring Boot 中的依赖注入Dependency Injection简称 DI是通过 Spring 框架的核心机制——控制反转Inversion of ControlIOC容器来实现的。Spring Boot 基于 Spring Framework在应用中自动进行对象的创建、管理、注入等工作开发者只需要声明依赖关系Spring 会自动将这些依赖注入到类中。
依赖注入的工作原理
在 Spring Boot 中依赖注入的核心概念是通过容器 ApplicationContext来管理和注入类的依赖。Spring 通过 注解来声明和管理这些依赖。
主要的注解有
Component / Service / Repository / Controller这些注解用于标记一个类为 Spring 管理的 Bean。Autowired用于标注类中的依赖变量告诉 Spring 自动注入相应的 Bean。Configuration 和 Bean用于配置类及其方法生成和管理 Bean。
依赖注入的工作流程
Bean 定义通过注解将类标记为 Spring 管理的 Bean如 ServiceComponent 等。自动注入使用 Autowired 注解将需要的 Bean 注入到类中。Spring Boot 会根据类型自动查找匹配的 Bean 并注入。容器管理Spring 会自动扫描指定的包通常是启动类所在的包及其子包根据注解发现类并把它们放入 IOC 容器中进行管理。生命周期管理Spring 管理这些 Bean 的生命周期包括实例化、依赖注入、初始化等。
代码案例Spring Boot 中的依赖注入
1. 创建 Bean 类
package com.hk.service;import org.springframework.stereotype.Service;Service
public class UserService {public String getUserInfo(String userId) {return User的id是: userId;}
}Service 注解将 UserService 类标记为一个 Spring 管理的 Bean。Spring Boot 会将 UserService 作为一个 Bean 加入到 Spring 容器中进行管理。
2. 依赖注入到其他类
package com.hk.controller;import com.hk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;RestController
public class UserController {private final UserService userService;// 通过构造器注入 UserServiceAutowiredpublic UserController(UserService userService) {this.userService userService;}GetMapping(/user/{userId})public String getUserInfo(PathVariable String userId) {// 使用注入的 UserService 实例return userService.getUserInfo(userId);}
}RestController 注解用于定义一个 REST 控制器是一个处理 HTTP 请求的组件。Autowired 注解用于自动注入UserService实例。在上面的代码中依赖注入是通过构造器进行的。 构造器注入 是推荐的方式它可以确保依赖关系在对象创建时就已经完全注入。构造器注入具有更高的可测试性和更好的不可变性。
3. 自动注入的其他方式
除了构造器注入Spring 还支持 字段注入 和 setter 注入。
a. 字段注入
RestController
public class UserController {Autowiredprivate UserService userService; // 字段注入GetMapping(/user/{userId})public String getUserInfo(PathVariable String userId) {return userService.getUserInfo(userId);}
}字段注入是将 Autowired 注解直接放在字段上Spring 会自动注入对应类型的 Bean。这种方式代码简洁但缺点是无法控制依赖注入的顺序且不容易进行单元测试。
b. Setter 注入
RestController
public class UserController {private UserService userService;Autowiredpublic void setUserService(UserService userService) {this.userService userService;}GetMapping(/user/{userId})public String getUserInfo(PathVariable String userId) {return userService.getUserInfo(userId);}
}Setter 注入通过 Autowired 注解标记在 setter 方法上Spring 会调用这个方法来注入依赖。适用于某些需要选择性注入的场景或者对于可选的依赖进行注入。
4. 使用 Qualifier 注解解决多个 Bean 的冲突
如果有多个类型相同的 BeanSpring 会根据类型来进行注入但如果类型不唯一会抛出 NoUniqueBeanDefinitionException 异常。在这种情况下我们可以使用 Qualifier 注解来指定注入哪一个 Bean。
假设我们有两个 UserService 的实现类
package com.hk.service;import org.springframework.stereotype.Service;Service(userServiceV1)
public class UserServiceV1 implements UserService {public String getUserInfo(String userId) {return User V1 的id是: userId;}
}Service(userServiceV2)
public class UserServiceV2 implements UserService {public String getUserInfo(String userId) {return User V2 的id是: userId;}
}使用 Qualifier 来指定注入的 Bean
package com.hk.controller;import com.hk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;RestController
public class UserController {private final UserService userService;Autowiredpublic UserController(Qualifier(userServiceV1) UserService userService) {this.userService userService;}GetMapping(/user/{userId})public String getUserInfo(PathVariable String userId) {return userService.getUserInfo(userId);}
}通过 Qualifier(userServiceV1) 注解我们指定了要注入 userServiceV1 Bean。
5. Primary 注解
如果有多个类型相同的 Bean且不想每次都使用 Qualifier 来指定注入哪个 Bean可以使用 Primary 注解来标记一个优先注入的 Bean。
package com.hk.service;import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;Service
Primary
public class UserServiceV1 implements UserService {public String getUserInfo(String userId) {return User V1 的id是: userId;}
}Primary 注解标记的 Bean 将会是默认注入的 BeanSpring 会优先选择它进行注入。
总结
在 Spring Boot 中依赖注入的工作原理是通过 Spring 容器管理对象的生命周期并将所需的依赖注入到类中。常见的注入方式包括构造器注入、字段注入和 setter 注入。Spring 使用 Autowired 注解来自动注入依赖通过 Component 和其衍生注解如 ServiceRepository 等标记 Bean。还可以通过 Qualifier 和 Primary 注解来解决多个 Bean 的冲突问题。