我知道在spring 2.5中引入@Component注释是为了通过使用类路径扫描来摆脱xml bean定义。

@Bean是在spring 3.0中引入的,可以和@Configuration一起使用,以便完全摆脱xml文件,而使用java配置。

是否有可能重用@Component注释而不是引入@Bean注释?我的理解是,最终目标是在这两种情况下都创建bean。


当前回答

You have two ways to generate beans. One is to create a class with an annotation @Component. The other is to create a method and annotate it with @Bean. For those classes containing method with @Bean should be annotated with @Configuration Once you run your spring project, the class with a @ComponentScan annotation would scan every class with @Component on it, and restore the instance of this class to the Ioc Container. Another thing the @ComponentScan would do is running the methods with @Bean on it and restore the return object to the Ioc Container as a bean. So when you need to decide which kind of beans you want to create depending upon current states, you need to use @Bean. You can write the logic and return the object you want. Another thing worth to mention is the name of the method with @Bean is the default name of bean.

其他回答

创建@Bean是为了避免在编译时耦合Spring和业务规则。这意味着您可以在其他框架(如PlayFramework或JEE)中重用业务规则。

此外,在默认的Spring实例化还不够的情况下,您可以完全控制如何创建bean。

我写了一篇关于它的文章。

https://coderstower.com/2019/04/23/factory-methods-decoupling-ioc-container-abstraction/

@ component 适用于元件扫描和自动布线。

什么时候应该使用@Bean?

有时自动配置不是一个选项。什么时候?让我们想象一下,您想要连接来自第三方库的组件(您没有源代码,所以不能用@Component注释它的类),因此自动配置是不可能的。

@Bean注释返回一个对象,spring应该在应用程序上下文中将该对象注册为bean。方法主体承担负责创建实例的逻辑。

我看到了很多答案,几乎每个地方都提到@Component用于自动装配组件,而@Bean恰恰是在声明要以不同的方式使用bean。让我来展示它的不同之处。

@ bean

首先,它是一个方法级注释。 其次,通常使用它在Java代码中配置bean(如果不使用xml配置),然后使用类从类中调用它 ApplicationContext。getBean方法。例子:

@Configuration
class MyConfiguration{
    @Bean
    public User getUser() {
        return new User();
    }
}

class User{
}    
        
// Getting Bean 
User user = applicationContext.getBean("getUser");

@ component

这是注释bean的一般方法,而不是专门的bean。 它是一个类级注释,用于避免通过java或xml配置进行所有配置。

我们得到这样的结果。

@Component
class User {
}

// to get Bean
@Autowired
User user;

就是这样。引入它只是为了避免实例化和使用该bean的所有配置步骤。

You have two ways to generate beans. One is to create a class with an annotation @Component. The other is to create a method and annotate it with @Bean. For those classes containing method with @Bean should be annotated with @Configuration Once you run your spring project, the class with a @ComponentScan annotation would scan every class with @Component on it, and restore the instance of this class to the Ioc Container. Another thing the @ComponentScan would do is running the methods with @Bean on it and restore the return object to the Ioc Container as a bean. So when you need to decide which kind of beans you want to create depending upon current states, you need to use @Bean. You can write the logic and return the object you want. Another thing worth to mention is the name of the method with @Bean is the default name of bean.

假设我想要特定的实现依赖于某种动态状态。 @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没有办法做到这一点。