0
votes

I read that Spring annotations like @Autowired, @Transactional & @PostConstruct are a form of / use BeanPostProcessors. So from what I understand about BeanPostProcessors, they are used to manage the lifecycle of a Spring Bean. Meaning you can specify any code which should be run before or after the intialization of a bean.

Now annotations like @Component or @Bean specify to Spring that it should create beans of this type. For example,

@Component
public Class Foo {

}

will tell spring to create a Bean of type "Foo" and then Spring will manage it's lifecycle. So does that make @Component and @Bean BeanPostProcessors?

2

2 Answers

2
votes

No! They are different.

  1. BeanPostProcessor is used to perform some operations before and after creating a bean,this allows us to add some code before and after creating the bean.

BeanPostProcessor class has two methods.

  • postProcessBeforeInitialization : It's used to make sure required actions are taken before initialization. e.g. you want to load certain property file/read data from the remote source/service.

  • postProcessAfterInitialization : It takes care of tasks that you want to do after initialization before bean reference is given to application.

Refer to : spring-doc-beans-customizing-using-BeanPostProcessor interface for details.


  1. @Component : It's the most generic Spring annotation.

A Java class decorated with @Component is found during classpath scanning and registered in the context as a Spring bean.

Refer to : spring-doc-component-annotation for details.

2
votes

I read that Spring annotations like @Autowired, @Transactional & @PostConstruct are a form of / use BeanPostProcessors.

Neither, it's actually the opposite. It is BeanPostProcessors that look for the annotations and react accordingly.

  • AutowiredAnnotationBeanPostProcessor - BeanPostProcessor implementation that autowires annotated fields, setter methods, and arbitrary config methods. Such members to be injected are detected through annotations: by default, Spring's @Autowired and @Value annotations.

    Also supports JSR-330's @Inject annotation, if available, as a direct alternative to Spring's own @Autowired.

  • CommonAnnotationBeanPostProcessor - BeanPostProcessor implementation that supports common Java annotations out of the box, in particular the JSR-250 annotations in the javax.annotation package. These common Java annotations are supported in many Java EE 5 technologies (e.g. JSF 1.2), as well as in Java 6's JAX-WS.

    This post-processor includes support for the PostConstruct and PreDestroy annotations - as init annotation and destroy annotation, respectively - through inheriting from InitDestroyAnnotationBeanPostProcessor with pre-configured annotation types.

Note that @Transactional is not handled by a BeanPostProcessor.


Now annotations like @Component or @Bean specify to Spring that it should create beans of this type. [...] So does that make @Component and @Bean BeanPostProcessors?

No.

@Component is an annotation that the component-scanning framework looks for to find beans to register. The component-scanning framework also looks for annotations that are themselves annotated with @Component, such as the @Configuration, @Controller, @Service, and @Repositiory annotations.

The @Bean annotation is then handled by a BeanDefinitionRegistryPostProcessor (not a BeanPostProcessor).

  • ConfigurationClassPostProcessor - BeanFactoryPostProcessor used for bootstrapping processing of @Configuration classes.

    This post processor is priority-ordered as it is important that any Bean methods declared in @Configuration classes have their corresponding bean definitions registered before any other BeanFactoryPostProcessor executes.