2

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

2 Answers 2

4

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

2
  • Thanks @Biju! I have updated my question. Thanks for clarifying. Jun 29, 2016 at 3:04
  • The behavior seems to be the same both with and without @Component. Are there any differences if I use Bean in class annotated with Component vs no annotation. Jun 29, 2016 at 3:11
3

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/

1
  • Thanks @Sundararaj! I have updated my question.Thanks for clarifying. Jun 29, 2016 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.