当前位置: 首页 > news >正文

Spring-boot读书笔记一@Component.vs.@bean

Component vs Bean in Spring Boot

Both are Spring-managed objects, but they differ in how and where they're defined.

@Component
Class-level annotation - marks the class itself as a Spring component

@Component
public class UserService {public void saveUser(String name) {// business logic}
}

Characteristics:

  • Applied directly to the class
  • Spring auto-detects during component scanning
  • One instance per class (unless specified otherwise)
  • Class controls its own creation

@Bean
Method-level annotation - creates beans through factory methods in @Configuration classes

@Configuration
public class AppConfig {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}@Beanpublic DatabaseService databaseService() {return new DatabaseService("localhost", 5432);}
}

Characteristics:

  • Applied to methods in @Configuration classes
  • You control object creation and configuration
  • Can create multiple beans from same class
  • External configuration of third-party classes

Key Differences
@Component @Bean
Class-level Method-level
Auto-detected Explicitly defined
Own classes Any classes (including third-party)
Simple creation Custom creation logic
One bean per class Multiple beans possible

When to Use Each

  • @Component for:

    1. Your own business classes
    2. Services, repositories, controllers
    3. Simple object creation
  • @Bean for:

    1. Third-party library objects
    2. Complex object configuration
    3. Conditional bean creation
    4. Multiple instances of same class

Both @Component and @Bean result in Spring-managed objects, but @Bean gives more control over the creation process.

http://www.zskr.cn/news/183785.html

相关文章:

  • 【2025最新】基于SpringBoot+Vue的线上学习资源智能推荐系统管理系统源码+MyBatis+MySQL
  • Miniconda环境下PyTorch模型降级回滚方案
  • 使用 K-Means 聚类进行图像分割
  • JLink驱动安装日志分析方法:快速定位错误原因
  • CCS20冗余架构设计原理:图解说明
  • STM32 USART波特率超详细版配置流程说明
  • Java Web 箱包存储系统系统源码-SpringBoot2+Vue3+MyBatis-Plus+MySQL8.0【含文档】
  • LTspice仿真错误排查技巧:常见报错通俗解释
  • Java SpringBoot+Vue3+MyBatis 项目申报管理系统系统源码|前后端分离+MySQL数据库
  • Miniconda-Python3.10镜像在诗歌生成大模型中的创意应用
  • Jupyter Notebook直连开发环境:Miniconda-Python3.10镜像使用图文教程
  • Miniconda-Python3.10镜像在虚拟偶像对话系统中的应用
  • Miniconda-Python3.10镜像在法律文书生成大模型中的应用
  • STM32与scanner传感器协同工作原理:通俗解释
  • Miniconda-Python3.10环境下运行HuggingFace Transformers示例
  • 使用Miniconda管理PyTorch模型的依赖生命周期
  • Miniconda环境下PyTorch模型混沌工程测试实践
  • 高效科研复现利器:Miniconda-Python3.10镜像助力AI实验稳定运行
  • 使用 JMeter 从 Fiddler 捕获请求并生成测试脚本(下)
  • 故障排查:Pytest Asyncio Event Loop Closed 错误
  • Miniconda环境下PyTorch模型热更新技术方案
  • Miniconda-Python3.10 + PyTorch实现百万级Token生成性能测试
  • Miniconda如何帮助开发者规避PyTorch版本陷阱
  • 图解说明multisim14.3下载安装步骤,清晰易懂零基础适用
  • Miniconda-Python3.10环境下安装Pandas进行数据清洗
  • 使用Miniconda为PyTorch项目构建可复现的基准环境
  • Meta 数十亿美元收购 Manus,肖弘将出任 Meta 副总裁
  • 基于Java+SpringBoot+SpringBoot大学生就业管理系统(源码+LW+调试文档+讲解等)/大学生就业平台/毕业生就业管理系统/高校就业管理系统/学生就业信息管理系统/就业服务管理系统
  • Miniconda-Python3.10镜像支持多种AI框架灵活切换
  • 基于Java+SpringBoot+SpringBoot家政服务与互助平台(源码+LW+调试文档+讲解等)/家政服务平台/互助服务平台/家政互助/家政服务网站/互助服务网站/家政与互助/家政互助系统