LuckyLuke
LuckyLuke

Reputation: 49107

BeanPostProcessor confusion

I am trying to understand BeanPostProcessor in Spring and I don't understand what it does. Is it correct that the BeanPostProcessor defines two methods that is called at these points:

Is that correct? Given the example and text on page 118 and further is confusing. I don't think I am allowed to copy more from the text to the question but the annotations and whats happening there is hard to understand.

And are you supposed to implement this interface on the beans you want or are you supposed to use this on a bean that is general for many beans? I see that you get both and object and string argument passed in.

Sometimes, you may find yourself in a position where you need to performsome additional processing immediately before and after Spring instantiates the bean. The processing can be as simple as modifying the bean or as complex as returning a completely different object! The BeanPostProcessor interface has two methods: postProcessBeforeInitialization, which is called before Spring calls any bean initialization hooks (such as InitializingBean.afterPropertiesSet or the init-method), and postProcessAfterInitialization, which Spring calls after the initialization hooks succeed.

Pro Spring 2.5, page 118

Upvotes: 10

Views: 8855

Answers (2)

omnomnom
omnomnom

Reputation: 9149

Spring gives you a lot of post processors, not only BeanPostProcessor. Also, most of them are used by the Spring itself. The one you mentioned in this question, is used (as its name states) to post process bean after its instantiation. Spring container behavior is as follows:

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

At the first sight, it may look complicated and overwhelming, but when you build complex applications on a top of Spring, all these features are just invaluable.

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

  • AutowiredAnnotationBeanPostProcessor - scans bean looking for @Autowire annotation in order to perform dependency injection
  • RequiredAnnotationBeanPostProcessor - checks if all dependencies marked as @Required has been injected.
  • ServletContextAwareProcessor - injects ServletContext to beans implementing ServletContextAware interface
  • actually, initialization/desctruction callbacks such as JSR-250 @PostConstruct and @PreDestroy are implemented using post processor: CommonAnnotationBeanPostProcessor

Of course all mentioned post processors must be executed in a specific order, but this is Spring responsibility to ensure it.

Upvotes: 21

David Harkness
David Harkness

Reputation: 36562

You implement BeanPostProcessor to build a service that applies to all beans in the context as they are created. The JavaDocs show many concrete examples but one I use consistently is AutowiredAnnotationBeanPostProcessor. This uses reflection to scan the bean class for @Autowired annotations on fields and methods.

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

This facility is most useful when building your own framework on top of Spring or features that apply across a subset of many beans. More likely you'll use the existing bean post-processors that Spring provides.

Upvotes: 7

Related Questions