
1. Overview
In this article, we will learn the ApplicationContextInitializer with an example.
It is a callback interface for initializing a Spring application context prior to being loaded.
Typically used within web applications that require some programmatic initialization of the application context. To learn more about Spring, refer to these articles.
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.
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.
3. ApplicationContextInitializer example
You must implement the ApplicationContextInitializer
to create a custom initializer that can perform additional initialization.
For example, the following code specifies the active profile as dev
in your Spring Environment
class.
public static class ProfileApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> { @Override public void initialize(GenericApplicationContext applicationContext) { ConfigurableEnvironment appEnvironment = applicationContext.getEnvironment(); appEnvironment.addActiveProfile("dev"); } }
3.1. Spring register bean using ApplicationContextInitializer
You can also register bean using the ApplicationContextInitializer
. To know the various other options to register beans programmatically, see this article.
In this class, you can call registerBean
and register the bean programmatically.
public class ProgrammaticApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> { @Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.registerBean(CustomBean.class, "Custom Bean initialization"); } }
4. Initialize the ApplicationContextInitalizer
If you are not using the Spring boot, then you can initialize the custom ApplicationContextInitalizer
as below:
public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanfactoryApplication.class); ProgrammaticApplicationContextInitializer programmaticApplicationContextInitializer = new ProgrammaticApplicationContextInitializer(); programmaticApplicationContextInitializer.initialize(applicationContext); }
If you are using Spring Boot, you can register it straightforwardly using either the SpringApplication
or SpringApplicationBuilder
:
@Configuration @EnableAutoConfiguration @ComponentScan public class BeanfactoryApplication { public static void main(String[] args) { new SpringApplicationBuilder(BeanfactoryApplication.class) .initializers(new ProgrammaticApplicationContextInitializer()) .run(args); // or // You can also add them on your SpringApplication before running it application.addInitializers(ProgrammaticApplicationContextInitializer.class); application.run(args); } }
You can also initialize them in META-INF/spring.factories
org.springframework.context.ApplicationContextInitializer=\ com.tedblob.ProgrammaticApplicationContextInitializer
You can add it to the web.xml
for a Spring web application by declaring a “contextInitializerClasses” context-param and init-param:
<context-param> <param-name>contextInitializerClasses</param-name> <param-value>com.tedblob.ProgrammaticApplicationContextInitializer</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
You can also add it to the external properties file such as application.properties
, application,yml
, so on:
context.initializer.classes=com.tedblob.ProgrammaticApplicationContextInitializer
5. Conclusion
To sum up, we have learned the ApplicationContextInitializer
with examples. You can find code samples of this article in our GitHub repository.