Understanding Spring Web Initialization
来源:互联网 发布:淘宝达人账号简介范本 编辑:程序博客网 时间:2024/06/11 00:27
Understanding Spring Web Initialization
Few years ago majority of us were used to write XML config files everywhere, to setup even simple Java EE application. Today using Java or Groovy to configure projects is becoming preferred way - you just need to take a look at Gradle or functionalities introduced in further versions of the Spring Framework to gen up on this.
Now I'll deal with configuring Spring contexts for web application.
Java EE provides ServletContainerInitializer interface, which allows libraries to be notified of a web application startup. SinceSpring 3.1 we have SpringServletContainerInitializer class which handlesWebApplicationInitializer by instantiating all found classes implementing this interface, sorting them basing on@Order annotation (non-annotated classes gets the highest possible order, so they are processed at the end) and invokingonStartup() method.
Spring since version 3.2 provides us a few classes implementing WebApplicationInitializer interface, from which first is AbstractContextLoaderInitializer. This class included in spring-web module uses abstractcreateRootApplicationContext() method to create application context, delegates it toContextLoaderListener which then is being registered in theServletContext instance. Creating application context using this class looks as follows:
That was the simplest way to start up Spring web context. But if we want to experience benefits provided by Spring MVC and don't want to manually register DispatcherServlet it'll be better to use another class: AbstractDispatcherServletInitializer. It extends previous class and adds two abstract methods: createServletApplicationContext() and getServletMappings(). First method returnsWebApplicationContext that will be passed to DispatcherServlet, which will be automatically added into container ServletContext. Please notice that this context will be established as a child of the context returned by createRootApplicationContext() method. Second method - as you have probably already deduced - returns mappings that are used during servlet registration. You can also override getServletFilters() method if you need any custom filters, because default implementation returns just empty array. Exemplary implementation using this class could be:
And now last but definitely not least class: AbstractAnnotationConfigDispatcherServletInitializer. Here we can see further step in simplifying Spring initialization - we don't need to manually create contexts but just set appropriate config classes in getRootConfigClasses() and getServletConfigClasses() methods. I hope you are already familiar with those names, because they works exactly like in the former case. Of course due to this class extends AbstractDispatcherServletInitializer we can still override getServletFilters(). Finally we can implement our configuration in the following way:
If you like to see wider context please follow examples in my GitHub repo: https://github.com/jkubrynski/spring-java-config-samples/
Now I'll deal with configuring Spring contexts for web application.
Java EE provides ServletContainerInitializer interface, which allows libraries to be notified of a web application startup. SinceSpring 3.1 we have SpringServletContainerInitializer class which handlesWebApplicationInitializer by instantiating all found classes implementing this interface, sorting them basing on@Order annotation (non-annotated classes gets the highest possible order, so they are processed at the end) and invokingonStartup() method.
Spring since version 3.2 provides us a few classes implementing WebApplicationInitializer interface, from which first is AbstractContextLoaderInitializer. This class included in spring-web module uses abstractcreateRootApplicationContext() method to create application context, delegates it toContextLoaderListener which then is being registered in theServletContext instance. Creating application context using this class looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
public
class
SpringAnnotationWebInitializer
extends
AbstractContextLoaderInitializer {
@Override
protected
WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext =
new
AnnotationConfigWebApplicationContext();
applicationContext.register(SpringAnnotationConfig.
class
);
return
applicationContext;
}
}
That was the simplest way to start up Spring web context. But if we want to experience benefits provided by Spring MVC and don't want to manually register DispatcherServlet it'll be better to use another class: AbstractDispatcherServletInitializer. It extends previous class and adds two abstract methods: createServletApplicationContext() and getServletMappings(). First method returnsWebApplicationContext that will be passed to DispatcherServlet, which will be automatically added into container ServletContext. Please notice that this context will be established as a child of the context returned by createRootApplicationContext() method. Second method - as you have probably already deduced - returns mappings that are used during servlet registration. You can also override getServletFilters() method if you need any custom filters, because default implementation returns just empty array. Exemplary implementation using this class could be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public
class
SpringWebMvcInitializer
extends
AbstractDispatcherServletInitializer {
@Override
protected
WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext =
new
AnnotationConfigWebApplicationContext();
applicationContext.register(SpringRootConfig.
class
);
return
applicationContext;
}
@Override
protected
WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext =
new
AnnotationConfigWebApplicationContext();
applicationContext.register(SpringMvcConfig.
class
);
return
applicationContext;
}
@Override
protected
String[] getServletMappings() {
return
new
String[]{
"/*"
};
}
}
And now last but definitely not least class: AbstractAnnotationConfigDispatcherServletInitializer. Here we can see further step in simplifying Spring initialization - we don't need to manually create contexts but just set appropriate config classes in getRootConfigClasses() and getServletConfigClasses() methods. I hope you are already familiar with those names, because they works exactly like in the former case. Of course due to this class extends AbstractDispatcherServletInitializer we can still override getServletFilters(). Finally we can implement our configuration in the following way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public
class
SpringWebMvcSimpleInitializer
extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected
Class<?>[] getRootConfigClasses() {
return
new
Class[] {SpringRootConfig.
class
};
}
@Override
protected
Class<?>[] getServletConfigClasses() {
return
new
Class[] {SpringMvcConfig.
class
};
}
@Override
protected
String[] getServletMappings() {
return
new
String[]{
"/*"
};
}
}
If you like to see wider context please follow examples in my GitHub repo: https://github.com/jkubrynski/spring-java-config-samples/
0 0
- Understanding Spring Web Initialization
- spring Context initialization failed
- java web :Context initialization failed
- Java Spring Tutorial -- Initialization & Destruction
- Understanding SOA with Web Services
- Spring Integration Message Bus Understanding
- initialization
- Initialization
- Initialization
- initialization
- Initialization
- [深度学习论文笔记][Weight Initialization] Understanding the difficulty of training deep feedforward neural
- Understanding
- Understanding ASP.NET AJAX Web Services
- Understanding User Goals in Web Search
- 1Understanding How Expression Web Works
- Some material form web for Understanding javascript
- Understanding Spring MVC Model and Session Attributes
- 算法笔记七:计数排序
- 作业报告10 多分段函数求值
- 第八周项目4求个人所得税
- jquery.validate.js的使用
- 为什么在jsp页面引入js插件一直报 is not a function
- Understanding Spring Web Initialization
- linux下oracle设置开机自启动实现方法
- MoiMark安卓中国终端体验性能排行榜(2014年10月)
- Log4net.config配置文件
- C++操作符的优先级
- 我的windows下的vim
- 第8周——定期存款利息计算器
- 我的编程学习日志(14)--八数码问题(代码)
- android签名机制