BeanPostProcessor Confusion

I am trying to understand BeanPostProcessor in Spring, and I do not understand what it is doing. Is it right that the BeanPostProcessor defines two methods that are called at these points:

  • Before initialization (either by init or afterPropertiesSet), but an instance is created.
  • After calling init or afterPropertiesSet

It is right? Given the example and the text on p. 118 onwards, this is confusing. I do not think that I am allowed to copy more from the text into the question, but the annotations and what is happening there are difficult to understand.

And you have to implement this interface on beans you want, or you have to use it on a bean, which is common to many beans? I see that you received both the object and the argument string and string.

Sometimes you may find yourself in a situation where you need to do additional processing immediately before and after Spring creates a bean instance. Processing can be as simple as changing a bean or as complex as returning a completely different object! The BeanPostProcessor interface has two methods: postProcessBeforeInitialization, which is called before any Spring calls, any bean initialization hooks (for example, InitializingBean.afterPropertiesSet or init-method), and postProcessAfterInitialization, which Spring calls after the initialization hosts are successful.

Pro Spring 2.5, p. 118

+8
java spring
source share
2 answers

Spring gives you a lot of mail processors, not just BeanPostProcessor . In addition, most of them are used by Spring itself. The one you mentioned in this question is used (as indicated in its title) to publish the bean process after it is created. Spring container behavior is as follows:

  • Spring creates a bean call to its constructor
  • postProcessBeforeInitialization(Object bean, String beanName) is called
  • bean initialization process: @PostConstruct , afterPropertiesSet() (defined by the InitializingBean callback interface), a custom init method
  • postProcessAfterInitialization(Object bean, String beanName) is called

At first glance, this may look complicated and overwhelming, but when you create complex applications on top of Spring, all these functions are simply invaluable.

Possible scenarios, for example (taken from Spring itself):

  • AutowiredAnnotationBeanPostProcessor - AutowiredAnnotationBeanPostProcessor bean looking for @Autowire annotation to perform dependency injection
  • RequiredAnnotationBeanPostProcessor - Checks if all dependencies are marked as @Required .
  • ServletContextAwareProcessor - injects ServletContext into beans implementation of the ServletContextAware interface
  • in fact, initialization / descriptive callbacks such as JSR-250 @PostConstruct and @PreDestroy are implemented using a post processor: CommonAnnotationBeanPostProcessor

Of course, all the mentioned post-processors should be executed in a certain order, but Spring is responsible for this.

+18
source share

You implement BeanPostProcessor to create a service that applies to all beans in context as they are created. JavaDocs shows many concrete examples, but I use AutowiredAnnotationBeanPostProcessor . This uses reflection to scan the bean class for @Autowired annotations by fields and methods.

 public class MyBean { @Autowired MyOtherBean otherBean; // assigned by AutowiredAnnotationBPP ... } 

This tool is most useful when creating your own structure on top of Spring or functions that apply to a subset of many beans. Most likely, you will use existing bean postprocessors that Spring provides.

+6
source share

All Articles