SpringBoot 使用 @Configuration 集中管理 Bean的实战步骤
作者:河阿里
在 SpringBoot 中,@Configuration 注解是专门用来集中管理 Bean 的核心方案,它可以替代传统 XML 配置文件,本文给大家介绍SpringBoot使用@Configuration集中管理 Bean的实战步骤,感兴趣的朋友跟随小编一起看看吧
在 SpringBoot 中,
@Configuration注解是专门用来集中管理 Bean 的核心方案,它可以替代传统 XML 配置文件,将所有第三方 Bean、自定义 Bean 统一在配置类中创建、管理,实现 Bean 的集中化、模块化管理。
一、核心概念
@Configuration:标记一个类为配置类,SpringBoot 启动时会自动扫描并加载这个类,替代 XML 配置文件。@Bean:写在配置类的方法上,方法的返回值就是一个 Bean 对象,方法名默认是 Bean 的名称。- 优势:集中管理所有 Bean、支持依赖注入、支持配置属性注入、代码可读性强、便于维护。
二、实战步骤
1. 基础环境
SpringBoot 项目(无需额外依赖,核心容器已包含 @Configuration)
2. 创建统一配置类
创建 config 包,编写配置类,集中注册所有需要的 Bean(自定义对象、第三方工具类等)。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 全局Bean配置类:集中管理所有Bean对象
*/
@Configuration // 核心注解:标记为配置类
public class GlobalBeanConfig {
// ==================== 1. 管理自定义 Bean ====================
/**
* 创建自定义 UserService Bean(也可以用 @Service 注解,这里演示配置类方式)
* @return Bean 对象
*/
@Bean // 方法名 userService 就是 Bean 的名称
public UserService userService() {
return new UserService();
}
// ==================== 2. 管理第三方工具类 Bean ====================
/**
* 注册线程池 Bean(第三方类,无法加 @Component,必须用配置类管理)
*/
@Bean
public ExecutorService threadPool() {
return Executors.newFixedThreadPool(5);
}
// ==================== 3. 带依赖注入的 Bean ====================
/**
* 创建 OrderService Bean,依赖上面的 userService Bean
*/
@Bean
public OrderService orderService(UserService userService) { // 自动注入已注册的 Bean
OrderService orderService = new OrderService();
// 手动注入依赖
orderService.setUserService(userService);
return orderService;
}
}3. 配套的业务类(无注解,纯Java类)
// 自定义业务类(无需加 @Service/@Component,由配置类统一注册)
public class UserService {
public String getUserInfo() {
return "用户信息:集中管理的Bean";
}
}
public class OrderService {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String getOrderInfo() {
return "订单信息,依赖:" + userService.getUserInfo();
}
}4. 测试使用集中管理的 Bean
直接通过 @Autowired 注入配置类中注册的 Bean 即可使用:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestController {
// 注入配置类中创建的 Bean
@Resource
private UserService userService;
@Resource
private OrderService orderService;
@Resource
private ExecutorService threadPool;
@GetMapping("/test")
public String test() {
// 测试自定义Bean
System.out.println(userService.getUserInfo());
// 测试依赖注入的Bean
System.out.println(orderService.getOrderInfo());
// 测试第三方Bean
threadPool.execute(() -> System.out.println("线程池Bean执行成功"));
return "Bean 集中管理测试成功";
}
}三、高级用法
1. 给 Bean 指定名称(默认是方法名)
@Bean(name = "customUserService") // 指定Bean名称
public UserService userService() {
return new UserService();
}
注入时指定名称:
@Resource(name = "customUserService") private UserService userService;
2. 配置多环境/模块化管理Bean
可以拆分多个配置类,按模块管理:
DataSourceConfig:管理数据库连接池 BeanThreadPoolConfig:管理线程池 BeanRedisConfig:管理 Redis 模板 Bean
SpringBoot 会自动扫描所有 @Configuration 类。
3. 读取配置文件注入 Bean
结合 @Value 读取 application.yml 配置,动态创建 Bean:
- application.yml
thread: core: 5
- 配置类中注入
import org.springframework.beans.factory.annotation.Value;
@Configuration
public class ThreadPoolConfig {
@Value("${thread.core}")
private Integer corePoolSize;
@Bean
public ExecutorService threadPool() {
return Executors.newFixedThreadPool(corePoolSize);
}
}4. Bean 销毁方法(资源释放)
适合线程池、连接池等需要关闭的 Bean:
@Bean(destroyMethod = "shutdown") // 指定销毁方法
public ExecutorService threadPool() {
return Executors.newFixedThreadPool(5);
}
四、总结
@Configuration= 配置文件:专门用于集中、统一管理 Bean,是 SpringBoot 推荐的无 XML 方案。@Bean= 注册 Bean:方法返回值就是 Spring 容器管理的对象。- 适用场景:
- 无法直接加
@Component的第三方类(线程池、数据源、工具类) - 需要自定义创建逻辑的 Bean
- 需要集中管理所有 Bean,统一维护
- 无法直接加
- 依赖注入:配置类中方法参数可以直接注入其他 Bean,Spring 自动匹配。
到此这篇关于SpringBoot 使用 @Configuration 集中管理 Bean的实战步骤的文章就介绍到这了,更多相关SpringBoot @Configuration 管理 Bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
