@Bean 和@ Component的区别
2024-10-19 07:35:04
- @Component auto detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean, rather than letting Spring do it automatically.
- @Component does not decouple the declaration of the bean from the class definition where as @Bean decouples the declaration of the bean from the class definition.
- @Component is a class level annotation where as @Bean is a method level annotation and name of the method serves as the bean name.
- @Component need not to be used with the @Configuration annotation where as @Bean annotation has to be used within the class which is annotated with @Configuration.
- We cannot create a bean of a class using @Component, if the class is outside spring container whereas we can create a bean of a class using @Bean even if the class is present outside the spring container.
- @Component has different specializations like @Controller, @Repository and @Service whereas @Bean has no specializations.
1. component : 1 利用spring 类路径扫描,
2. 写在类上,和类定义不分离。
3. 不需要配合 @configuration注解
2. Bean : .当类不受spring控制的时候,写一个方法,返回某个不受spring控制的类的对象。 这样就能在spring里注入一个bean。
配合configuration注解
参考:https://stackoverflow.com/questions/10604298/spring-component-versus-bean
最新文章
- [转]VS2012 快捷键
- JDK6环境下升级项目到springframework4.x和tomcat7.x
- 有关在线OJ网络AC爬虫
- Javascript 基础--数组
- IntelliJ IDEA14 安装
- python基础之元组(Tuple)、字典(Dictionary)详解
- Nah Lock: 一个无锁的内存分配器
- 使用Image.GetThumbnailImage 方法返回缩略图
- vue vuex 提交 this.$store.commit({type: 'setSelectPro', selectPro: this.productId});
- Java多线程:synchronized关键字和Lock
- SQL SERVER 2012 AlwaysOn - 维护篇 03
- mac下安装gradle
- 使用kqueue的str_cli函数
- 了解计算机与操作系统发展阶段--Windows
- 彻底解决CDH5.12安装过程中 Error: JAVA_HOME is not set and Java could not be found
- Cisco端口限速配置
- Android流媒体开发之路一:Camera2采集摄像头原始数据并手动预览
- linux的基本操作3(权限)
- 好吧,我承认我是爱瞎折腾----利用YDUI改变页面UI
- Linux centos7下php安装cphalcon扩展的方法