@Component,@Bean
注解分类
- @Controller: controller控制器层(注入服务),用于标注控制层组件
- @Service:service服务层(注入dao),用于标注业务层组件
- @Repository:dao持久层(实现dao访问),用于标注数据访问组件,即DAO组件
- @Component: 标注一个类为Spring容器的Bean,(把普通pojo实例化到spring容器中,相当于配置文件中的),泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注,标识为一个Bean。
@Component 注解含义
@Component: 标注Spring管理的Bean,使用@Component注解在一个类上,表示将此类标记为Spring容器中的一个Bean。@Component作用就相当于 XML配置。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//package org.springframework.stereotype;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {String value() default "";
}
@Component 和 @Bean 的区别
1、两者的联系和区别
@Component 和 @Bean 是两种使用注解来定义bean的方式。
@Component
(和@Service
和@Repository
)用于自动检测和使用类路径扫描自动配置bean。注释类和bean之间存在隐式的一对一映射(即每个类一个bean)。@Component作用就相当于 XML配置
这种方法对需要进行逻辑处理的控制非常有限,因为它纯粹是声明性的。
@Bean
用于显式声明单个bean,而不是让Spring像上面那样自动执行它。它将bean的声明与类定义分离,并允许您精确地创建和配置bean。
@Component
public class Student {private String name = "lkm";public String getName() {return name;}public void setName(String name) {this.name = name;}
}
而@Bean则常和@Configuration注解搭配使用
@Configuration
public class WebSocketConfig {@Beanpublic Student student(){return new Student();}}
都可以使用@Autowired或者@Resource注解注入
@Autowired
Student student;
2、为什么有了@Compent,还需要@Bean
如果想将第三方的类变成组件,你又没有没有源代码,也就没办法使用@Component
进行自动配置,这种时候使用@Bean
就比较合适了。不过同样的也可以通过xml方式来定义。
另外@Bean注解的方法返回值是对象,可以在方法中为对象设置属性。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//package org.springframework.context.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.core.annotation.AliasFor;@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {@AliasFor("name")String[] value() default {};@AliasFor("value")String[] name() default {};/** @deprecated */@DeprecatedAutowire autowire() default Autowire.NO;boolean autowireCandidate() default true;String initMethod() default "";String destroyMethod() default "(inferred)";
}
@Component,@Bean相关推荐
- @bean注解和@component注解的区别_通过源码查看 @Component,@Service 等注解是如何被解析的...
点击上方"匠心零度",选择"设为星标" 做积极的人,而不是积极废人 来源:my.oschina.net/floor/blog/4325651 前言 1.@Com ...
- Spring 注解比较,@Bean 和 @Component的区别
点击下方"IT牧场",选择"设为星标" 本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较, ...
- Spring中的@ Component,@ Repository和@Service批注有什么区别?
@Repository @Component , @Repository和@Service批注可以在Spring中互换使用吗,或者除了充当注解设备外,它们还提供任何特定功能吗? 换句话说,如果我有一个 ...
- 面试官:注解@Component,@Service是如何被解析的?
点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐19个github超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 来源: https://my ...
- spring中stereotype注解Component,Repository,Service,Controller
[README] 本文介绍了 spring4.0 下 org.springframework.stereotype 的注解类型,俗称刻板型注解(一成不变型): 包括 @Component, @Repo ...
- r中rep_Spring中@ Component,@ Service,@ Controller和@Repository之间的区别
r中rep 在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repositor ...
- Spring中@ Component,@ Service,@ Controller和@Repository之间的区别
在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repository批注之间的 ...
- 框架源码专题:springIOC的加载过程,bean的生命周期,结合spring源码分析
文章目录 1.BeanFactory和ApplicationContext的区别? 2. IOC与 Bean的加载过程 ①:初始化容器DefaultListableBeanFactory ②:创建读取 ...
- @Component,@Service等注解是如何被解析的?
来源:my.oschina.net/floor/blog/4325651 前言 @Component和@Service都是工作中常用的注解,Spring如何解析? 1.@Component解析流程 找 ...
最新文章
- 浅谈企业IT应用的访问方式之:乱想
- [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期
- 发生在“注解”@的那些事儿
- 数据库工作笔记/设计思路-通过增加索引表为某库中其他表添加索引(描述信息)
- Flutter 是移动应用程序开发的未来?
- 树莓派boot分区cmdline.txt
- 使用Git和远程代码库
- Ubunt 14.04 install Sogou input
- [转载]Matlab定积分近似计算
- python docx创建表格
- 烟台移动dns网关_各地移动dns列表
- 2021-11-01 讲题题解
- [SIGMOD 2021] SharPer: Sharding Permissioned Blockchains Over Network Clusters
- 关于RHCE考证的那些事
- C++ Qt 05:Qt布局管理器 - 荒 木 - 博客园
- KODI | 智能电视系统
- 数字图像处理-常考算法代码-详细注释
- 三层架构与四大天王之——改
- AdminLTE模板框架
热门文章
- wget 下载百度云jdk
- mui toast自定义样式
- 创建loop15设备挂载镜像文件(.img)
- Gstreamer之No package ‘gstreamer-1.0‘ found解决(十一)
- Glib2之dbus用法(五)
- Docker容器中运行Ubuntu系统
- reinterpret_cast 最小demo
- ios build fdk-aac
- android之下载文件
- android之NDK version was not found