Spring WebApplicationInitializer and ApplicationContextInitializer confusion



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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值