我知道在spring 2.5中引入@Component注释是为了通过使用类路径扫描来摆脱xml bean定义。
@Bean是在spring 3.0中引入的,可以和@Configuration一起使用,以便完全摆脱xml文件,而使用java配置。
是否有可能重用@Component注释而不是引入@Bean注释?我的理解是,最终目标是在这两种情况下都创建bean。
我知道在spring 2.5中引入@Component注释是为了通过使用类路径扫描来摆脱xml bean定义。
@Bean是在spring 3.0中引入的,可以和@Configuration一起使用,以便完全摆脱xml文件,而使用java配置。
是否有可能重用@Component注释而不是引入@Bean注释?我的理解是,最终目标是在这两种情况下都创建bean。
当前回答
Bean和Component的区别:
其他回答
当您使用@Component标记时,这与使用带有香草bean声明方法(用@Bean注释)的POJO(普通旧Java对象)是一样的。例如,下面的方法1和2将给出相同的结果。
方法1
@Component
public class SomeClass {
private int number;
public SomeClass(Integer theNumber){
this.number = theNumber.intValue();
}
public int getNumber(){
return this.number;
}
}
用豆子表示“数字”:
@Bean
Integer theNumber(){
return new Integer(3456);
}
方法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;
}
}
两者都有豆子:
@Bean
Integer theNumber(){
return new Integer(3456);
}
@Bean
SomeClass someClass(Integer theNumber){
return new SomeClass(theNumber);
}
方法2允许您将bean声明放在一起,它更加灵活。您甚至可能想要添加另一个非香草SomeClass bean,如下所示:
@Bean
SomeClass strawberryClass(){
return new SomeClass(new Integer(1));
}
假设我想要特定的实现依赖于某种动态状态。 @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();
}
}
然而,@Component没有办法做到这一点。
@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 whereas @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.
创建@Bean是为了避免在编译时耦合Spring和业务规则。这意味着您可以在其他框架(如PlayFramework或JEE)中重用业务规则。
此外,在默认的Spring实例化还不够的情况下,您可以完全控制如何创建bean。
我写了一篇关于它的文章。
https://coderstower.com/2019/04/23/factory-methods-decoupling-ioc-container-abstraction/
1. 关于@ component @Component的功能类似于@Configuration。 它们都表明带注释的类有一个或多个需要注册到Spring-IOC-Container的bean。 由@Component注释的类,我们称之为Spring的Component。它是一个包含多个bean的概念。 Spring需要自动扫描组件类来注册组件类的那些bean。
2. 关于@ bean @Bean用于注释组件类的方法(如上所述)。它表示由带注释的方法返回的实例需要注册到Spring-IOC-Container。
3.结论 两者的区别是比较明显的,它们在不同的情况下使用。 一般用法是:
// @Configuration is implemented by @Component
@Configuration
public ComponentClass {
@Bean
public FirstBean FirstBeanMethod() {
return new FirstBean();
}
@Bean
public SecondBean SecondBeanMethod() {
return new SecondBean();
}
}