Home » Difference ApplicationContextInitializer and BeanFactoryPostProcessor

Difference ApplicationContextInitializer and BeanFactoryPostProcessor

  • by
Difference between ApplicationContextInitializer and BeanFactoryPostProcessor

1. Overview

In this article, we will learn the difference between ApplicationContextInitializer and BeanFactoryPostProcessor.

2. ApplicationContextInitializer

The ApplicationContextInitializer allows you to do additional initialization before loading the persistent bean definition (e.g. your application-context.xml). It is essentially code that gets executed before the Spring application context gets completely created.

The ApplicationContextInitializer is a callback interface for initializing a Spring application context prior to being loaded.

For example, registering property sources or activating profiles against the context’s environment.

The ApplicationContextInitializer contains a single abstract method initialize:

void initialize(C applicationContext)
//Initialize the given application context.

Typically used within web applications that require some programmatic initialization of the application context. To learn more about ApplicationContextInitializer, refer to this article.

3. BeanFactoryPostProcessor

The ApplicationContext auto-detects BeanFactoryPostProcessor beans in its bean definitions and apply them before any other beans get created. However, you can also register the BeanFactoryPostProcessor programmatically with a ConfigurableApplicationContext.

This BeanFactoryPostProcessor is an extension point to customize the bean definitions or configuration metadata of the bean before the creation of the beans.

For example, the PropertyPlaceholderConfigurer which replaces placeholders with concrete values.

4. Conclusion

To sum up, we have learned the difference between ApplicationContextInitializer and BeanFactoryPostProcessor. You can find code samples of this article in our GitHub repository.