Spring WebApplicationInitializer and ApplicationContextInitializer confusion
2014-10-20 11:03
375 查看
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:
view source
print?
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:
view source
print?
If you have a Spring-Boot based application then registering an ApplicationContextInitializer is fairly straightforward:
view source
print?
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:
view source
print?
If it a normal web.xml configuration then the initializer can be specified this way:
view source
print?
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.
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:
view source
print?
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:
view source
print?
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:
view source
print?
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:
view source
print?
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:
view source
print?
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.
相关文章推荐
- Spring异常:Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.
- Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- Initializing Spring root WebApplicationContext
- spring XmlWebApplicationContext详解
- spring+hibernate整合web.xml、applicationContext.xml配置
- Spring WebApplicationContext
- Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- spring XmlWebApplicationContext详解
- Spring中的WebApplicationContext
- (Portal 开发读书笔记)Portlet中的Spring Web Application Context
- 我的第一篇:浅析spring中的webApplicationContext
- Struts2获取Spring的WebApplicationContext
- Spring 配置问题还是代码问题?Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
- strut2 call spring WebApplicationContext
- web工程之启动时spring的dtd错误 [/WEB-INF/applicationContext.xml
- tomcat 启动时 Initializing Spring root WebApplicationContext停止启动
- 从源码看spring applicationContext在web容器中加载过程
- javax.servlet.UnavailableException: org/springframework/web/context/WebApplicationContext
- web.xml & Spring applicationContext.xml简单配置(备忘)