Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

java - Spring - Are annotations like @Component BeanPostProcessors

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?

question from:https://stackoverflow.com/questions/65870992/spring-are-annotations-like-component-beanpostprocessors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...