These are two concepts that I mix up occasionally - a WebApplicationInitializer and an ApplicationContextInitializer, and wanted to describe each of them to clarify them for myself.
I have previously blogged about WebApplicationInitializerhere and here. It is relevant purely in a Servlet 3.0+ spec compliant servlet container and provides a hook to programmatically configure the servlet context. How does this help - you can have a web application without potentially any web.xml file, typically used in a Spring based web application to describe the root application context and the Spring web front controller called theDispatcherServlet. An example of using WebApplicationInitializer is the following:
01.
public
class
CustomWebAppInitializer
extends
AbstractAnnotationConfigDispatcherServletInitializer {
02.
@Override
03.
protected
Class<?>[] getRootConfigClasses() {
04.
return
new
Class<?>[]{RootConfiguration.
class
};
05.
}
06.
07.
@Override
08.
protected
Class<?>[] getServletConfigClasses() {
09.
return
new
Class<?>[]{MvcConfiguration.
class
};
10.
}
11.
12.
@Override
13.
protected
String[] getServletMappings() {
14.
return
new
String[]{
"/"
};
15.
}
16.
}
Now, what is an ApplicationContextInitializer. It is essentially code that gets executed before the Spring application context gets completely created. A good use case for using an ApplicationContextInitializer would be to set a Spring environment profile programmatically, along these lines:
01.
public
class
DemoApplicationContextInitializer
implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
02.
03.
@Override
04.
public
void
initialize(ConfigurableApplicationContext ac) {
05.
ConfigurableEnvironment appEnvironment = ac.getEnvironment();
06.
appEnvironment.addActiveProfile(
"demo"
);
07.
08.
}
09.
}
If you have a Spring-Boot based application then registering an ApplicationContextInitializer is fairly straightforward:
01.
@Configuration
02.
@EnableAutoConfiguration
03.
@ComponentScan
04.
public
class
SampleWebApplication {
05.
06.
public
static
void
main(String[] args) {
07.
new
SpringApplicationBuilder(SampleWebApplication.
class
)
08.
.initializers(
new
DemoApplicationContextInitializer())
09.
.run(args);
10.
}
11.
}
For a non Spring-Boot Spring application though, it is a little more tricky, if it is a programmatic configuration of web.xml, then the configuration is along these lines:
01.
public
class
CustomWebAppInitializer
implements
WebApplicationInitializer {
02.
03.
@Override
04.
public
void
onStartup(ServletContext container) {
05.
AnnotationConfigWebApplicationContext rootContext =
new
AnnotationConfigWebApplicationContext();
06.
rootContext.register(RootConfiguration.
class
);
07.
ContextLoaderListener contextLoaderListener =
new
ContextLoaderListener(rootContext);
08.
container.addListener(contextLoaderListener);
09.
container.setInitParameter(
"contextInitializerClasses"
,
"mvctest.web.DemoApplicationContextInitializer"
);
10.
AnnotationConfigWebApplicationContext webContext =
new
AnnotationConfigWebApplicationContext();
11.
webContext.register(MvcConfiguration.
class
);
12.
DispatcherServlet dispatcherServlet =
new
DispatcherServlet(webContext);
13.
ServletRegistration.Dynamic dispatcher = container.addServlet(
"dispatcher"
, dispatcherServlet);
14.
dispatcher.addMapping(
"/"
);
15.
}
16.
}
If it a normal web.xml configuration then the initializer can be specified this way:
1.
<
context-param
>
2.
<
param-name
>contextInitializerClasses</
param-name
>
3.
<
param-value
>com.myapp.spring.SpringContextProfileInit</
param-value
>
4.
</
context-param
>
5.
6.
<
listener
>
7.
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
8.
</
listener
>
So to conclude, except for the Initializer suffix, both WebApplicationInitializer and ApplicationContextInitializer serve fairly different purposes. Whereas the WebApplicationInitializer is used by a Servlet Container at startup of the web application and provides a way for programmatic creating a web application(replacement for a web.xml file), ApplicationContextInitializer provides a hook to configure the Spring application context before it gets fully created.