2

How is defining a bean in the JavaConfig different then just annotating a class?

JavaConfig:

@Configuration
public class AppConfig {
    @Bean
    public FilterRegistrationBean filterRegistrationBean () {
        SomeFilter filter = new SomeFilter();

        return SomeFilter;
    }

Annotated Class

@Bean
public class SomeFilter extends FilterRegistrationBean {
}
Steve
  • 53,375
  • 33
  • 96
  • 141

2 Answers2

5

The @Bean annotation cannot be applied to a class type. @Bean is an annotation processed by a @Configuration class parser. It applies to factory methods.

Assuming you meant something like @Component, the difference is explicit declaration, where you control the whole instantiation of the bean class and its initialization, versus implicit, where you let the container figure it out.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

You can annotate your bean with say @Component to achieve the same effect as with the @Bean annotation in the Config class. I.e. @Bean explicit bean declaration. @Component implicit (or automatic) bean definition.

Julio
  • 720
  • 7
  • 16