本文翻译自:Spring: @Component versus @Bean

I understand that @Component annotation was introduced in spring 2.5 in order to get rid of xml bean definition by using classpath scanning. 我了解到@Component注释是在Spring 2.5中引入的,目的是通过使用类路径扫描摆脱xml bean的定义。

@Bean was introduced in spring 3.0 and can be used with @Configuration in order to fully get rid of xml file and use java config instead. @Bean是在Spring 3.0中引入的,可以与@Configuration一起使用,以完全摆脱xml文件并改用Java config。

Would it have been possible to re-use the @Component annotation instead of introducing @Bean annotation? 是否有可能重新使用@Component注释而不是引入@Bean注释? My understanding is that the final goal is to create beans in both cases. 我的理解是,两种情况下的最终目标都是创建bean。


#1楼

参考:https://stackoom.com/question/iUf4/春季-Component与-Bean


#2楼

@Component and @Bean do two quite different things, and shouldn't be confused. @Component@Bean做两个完全不同的事情,不要混淆。

@Component (and @Service and @Repository ) are used to auto-detect and auto-configure beans using classpath scanning. @Component (以及@Service@Repository )用于使用类路径扫描自动检测和自动配置bean。 There's an implicit one-to-one mapping between the annotated class and the bean (ie one bean per class). 在带注释的类和Bean之间存在隐式的一对一映射(即,每个类一个Bean)。 Control of wiring is quite limited with this approach, since it's purely declarative. 由于此布线仅是声明性的,因此使用此方法对布线的控制非常有限。

@Bean is used to explicitly declare a single bean, rather than letting Spring do it automatically as above. @Bean用于显式声明单个bean,而不是像上面那样让Spring自动执行。 It decouples the declaration of the bean from the class definition, and lets you create and configure beans exactly how you choose. 它使Bean的声明与类定义脱钩,并让您完全按照自己的选择创建和配置Bean。

To answer your question... 要回答您的问题...

would it have been possible to re-use the @Component annotation instead of introducing @Bean annotation? 是否有可能重新使用@Component注释而不是引入@Bean注释?

Sure, probably; 当然可以; but they chose not to, since the two are quite different. 但他们选择不这样做,因为两者是完全不同的。 Spring's already confusing enough without muddying the waters further. 春天已经足够混乱了,又没有使水进一步混乱。


#3楼

Let's consider I want specific implementation depending on some dynamic state. 让我们考虑根据某些动态状态来实现特定的实现。 @Bean is perfect for that case. @Bean非常适合这种情况。

@Bean
@Scope("prototype")
public SomeService someService() {switch (state) {case 1:return new Impl1();case 2:return new Impl2();case 3:return new Impl3();default:return new Impl();}
}

However there is no way to do that with @Component . 但是,无法使用@Component做到这一点。


#4楼

@Component Preferable for component scanning and automatic wiring. @Component优选用于组件扫描和自动接线。

When should you use @Bean ? 什么时候应该使用@Bean

Sometimes automatic configuration is not an option. 有时,自动配置不是一个选择。 When? 什么时候? Let's imagine that you want to wire components from 3rd-party libraries (you don't have the source code so you can't annotate its classes with @Component), so automatic configuration is not possible. 假设您想从第三方库连接组件(您没有源代码,因此不能使用@Component注释其类),因此无法进行自动配置。

The @Bean annotation returns an object that spring should register as bean in application context. @Bean批注返回一个对象该对象应在应用程序上下文中作为Spring注册为bean。 The body of the method bears the logic responsible for creating the instance. 方法主体具有负责创建实例的逻辑。


#5楼

Both approaches aim to register target type in Spring container. 两种方法都旨在在Spring容器中注册目标类型。

The difference is that @Bean is applicable to methods , whereas @Component is applicable to types . 不同之处在于@Bean适用于方法 ,而@Component适用于类型

Therefore when you use @Bean annotation you control instance creation logic in method's body (see example above ). 因此,当您使用@Bean批注时,您可以控制方法主体中的实例创建逻辑(请参见上面的示例 )。 With @Component annotation you cannot. 使用@Component注释不能。


#6楼

When you use the @Component tag, it's the same as having a POJO (Plain Old Java Object) with a vanilla bean declaration method (annotated with @Bean ). 当您使用@Component标记时,它与使用香草豆声​​明方法(以@Bean注释)的POJO(普通的旧Java对象) @Bean For example, the following method 1 and 2 will give the same result. 例如,下面的方法1和2将给出相同的结果。

Method 1 方法1

@Component
public class SomeClass {private int number;public SomeClass(Integer theNumber){this.number = theNumber.intValue();}public int getNumber(){return this.number;}
}

with a bean for 'theNumber': 与“ theNumber”的bean:

@Bean
Integer theNumber(){return new Integer(3456);
}

Method 2 方法二

//Note: no @Component tag
public class SomeClass {private int number;public SomeClass(Integer theNumber){this.number = theNumber.intValue();}public int getNumber(){return this.number;}
}

with the beans for both: 与两个豆:

@Bean
Integer theNumber(){return new Integer(3456);
}@Bean
SomeClass someClass(Integer theNumber){return new SomeClass(theNumber);
}

Method 2 allows you to keep bean declarations together, it's a bit more flexible etc. You may even want to add another non-vanilla SomeClass bean like the following: 方法2允许您将bean声明保持在一起,这更加灵活等等。您甚至可能想要添加另一个非香草的SomeClass bean,如下所示:

@Bean
SomeClass strawberryClass(){return new SomeClass(new Integer(1));
}

春季:@Component与@Bean相关推荐

  1. @Component,@Bean

    注解分类 @Controller: controller控制器层(注入服务),用于标注控制层组件 @Service:service服务层(注入dao),用于标注业务层组件 @Repository:da ...

  2. SSM-Spring-Spring装配Bean-通过注解装配Bean-使用@Component装配Bean

    SSM-Spring-Spring装配Bean-通过注解装配Bean-使用@Component装配Bean ​ 使用这种方式可以减少XML的配置,注解功能更加强大,可以实现XML的功能,还有自动装配的 ...

  3. @Component 和 @Bean 的区别

    Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean. 完成这两个动作有三种方式,一种是使用自动配置的方式.一种是使用JavaConfig的方式,一种就是使用XML配置的 ...

  4. @Component和@Bean的区别

    最近学习spring的时候,一直搞不清这两个的区别,网上写的是在太官方了,小白太难了.于是自己整理一下. 首先,相同点,这两者目的都是注册bean到spring中因此都可以通过@Autowried装配 ...

  5. 一次性讲清 Spring 常用注解 @Bean 、 @Component 、@Autowire、@Resource 的区别, 你知道吗?

    本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较,@ControllerAdvice 是如何处理自定义异常的等等. Spring ...

  6. Spring 注解 @bean 和 @component 的区别, 你知道吗?

    本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较,@ControllerAdvice 是如何处理自定义异常的等等. Spring ...

  7. @Bean+@Component+@Configuration+@Autowired的配合使用与区别(转载+整理+完整实验)

    大概是这么几种用法: 组合使用 示例代码 代理 @Configuration+@bean 所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例. @Component+@b ...

  8. Spring 注解比较,@Bean 和 @Component的区别

    点击下方"IT牧场",选择"设为星标" 本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较, ...

  9. Spring中的@ Component,@ Repository和@Service批注有什么区别?

    @Repository @Component , @Repository和@Service批注可以在Spring中互换使用吗,或者除了充当注解设备外,它们还提供任何特定功能吗? 换句话说,如果我有一个 ...

最新文章

  1. axure 下拉多选 元件_Axure教程:下拉多选列表集合(多选下拉列表+单选下拉列表+分级下拉列表)...
  2. 再学 GDI+[25]: TGPPen - 宽度、颜色与线帽
  3. MFC 常用系统函数
  4. Go map[int64]int64 写入 redis 占用多少内存
  5. Spring中的类型转换
  6. 算法题-字符串匹配算法
  7. Jmeter插件监控服务器性能
  8. python赋值与c语言区别,运算符-赋值运算符和逻辑运算符
  9. python gui哪个好看_python的GUI选择什么方案比较好?
  10. Linux 进程间通信 无名管道(pipe)
  11. 计算机感染冲击波,CIH、爱虫、冲击波、熊猫烧香,对这4种网络病毒你了解多少?...
  12. 2021-2022-1 线性代数知识点总结
  13. PB的特点及Powerscript的语言基础
  14. 进一步解析ie环境下z-index问题解决方法
  15. 科学计算机已知角度和边长怎样算斜长,计算公式
  16. 苹果怎么测是原装屏_Wendy在华强北教你组装苹果8P——屏幕
  17. HTML转义字符对照表(部分)
  18. 数据库之十二星座 --- 双鱼座的复杂关系
  19. Entity Framework 一对多关系映射
  20. 【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、Accuracy、Recall、IoU、mIoU

热门文章

  1. ASP.NET内置对象二
  2. cisco和H3C命令对比
  3. firefox 插件可能用得上的Firefox插件及下载
  4. Migrate blog from blogcn here
  5. hdu 4279Number(数论)
  6. 使用Xcode7的Instruments检测解决iOS内存泄露
  7. 利用IP地址查询接口来查询IP归属地
  8. Redhat 7搭建iscsi存储系统
  9. Zabbix 对接 LDAP 实现用户统一登录的方法
  10. Zabbix监控Redis状态