Punter Vicky
Punter Vicky

Reputation: 17032

Spring @Component & @Bean annotation

I believe @Configuration annotation when used in conjunction with @Bean annotation in spring is used to replace xml configuration. However I saw a piece of code where @Bean was used in conjunction with @Component (defined at class level). Is this a valid declaration? Are there any any pros / cons in using @Component with @Bean annotation vs using @Configuration and @Bean.

EDIT:

Thanks @Sundar & @Biju. I did programmatic call between 2 bean methods under Component class. I saw different object values. However when I used Configuration , I saw the same bean values. Based on what you had explained , I assume a regular method call was made when I used @Component , whereas when I used @Configuration , I assume method annotated with @Bean was treated as a Spring Bean

Code

@Component
public class AppConfig {

    @Bean(name="customerService")
    public CustomerService getCustomerService(){
        System.out.println(getService());
        System.out.println(getService());
        return getService();
    }

    @Bean
    public CustomerService getService(){
        return new CustomerServiceImpl();
    }
}

Console Output

com.company.service.CustomerServiceImpl@68bbe345
com.company.service.CustomerServiceImpl@30b8a058

Code

@Configuration
public class AppConfig {

    @Bean(name="customerService")
    public CustomerService getCustomerService(){
        System.out.println(getService());
        System.out.println(getService());
        return getService();
    }

    @Bean
    public CustomerService getService(){
        return new CustomerServiceImpl();
    }
}

Console Output

com.company.service.CustomerServiceImpl@71623278
com.company.service.CustomerServiceImpl@71623278

Upvotes: 2

Views: 10208

Answers (3)

Nihar
Nihar

Reputation: 1

https://www.learnjavaupdate.com/2023/04/difference-between-bean-and-component.html

  1. Purpose: @Bean is used to explicitly declare a single bean. It is typically used to configure third-party objects that are not under the control of Spring, or to create beans that require some customization beyond what can be achieved using component scanning. On the other hand, @Component is used to automatically detect and register all classes marked as components in the application context.

  2. Configuration: Beans defined using @Bean are typically created in configuration classes that are annotated with @Configuration. These classes contain methods that return instances of the beans that are to be managed by the Spring container. In contrast, @Component is used to mark any class as a Spring-managed component that can be injected into other objects.

  3. Scoping: @Bean can be used to configure bean scope, while @Component does not offer this feature. This means that beans created using @Bean can be defined with specific scopes, such as prototype or singleton, whereas beans created using @Component are typically singletons by default.

  4. Flexibility: @Bean provides greater flexibility than @Component, since it allows for programmatic instantiation and configuration of the bean, as well as more control over the bean's lifecycle. On the other hand, @Component is simpler and more declarative, allowing Spring to automatically detect and manage the components without requiring any explicit configuration.

Upvotes: -1

Sundararaj Govindasamy
Sundararaj Govindasamy

Reputation: 8495

You can use @Component as an alternative for @Configuration. It’s official suggestion from spring team.

Simply declare your @Bean methods on classes not annotated with @Configuration (but typically with another Spring stereotype instead, e.g. @Component). As long as you don’t do programmatic calls between your @Bean methods, this is going to work just as fine, but conditions apply*.

Please refer more info in this link.

http://dimafeng.com/2015/08/29/spring-configuration_vs_component/

Upvotes: 3

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

It is a valid declaration, however there are catches - the one within a @Component is referred to as a lite-mode and dependencies cannot easily be injected for beans declared in this form. The recommendation is always to use @Bean in a @Configuration annotated class - here is a good reference on this - http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-basic-concepts

Upvotes: 4

Related Questions