spring与servlet3.0无web.xml文件时的配置方式
2016-01-13 00:00
597 查看
摘要: spring webmvc servlet 3.0 web.xml ; spring在使用servlet3.0时的配置方式
这里只做简要的记录,更详细的使用方式,更详细的资料参见spring参考文档(21.15 Code-based Servlet container initialization),以后会慢慢补充。
21.15 Code-based Servlet container initialization
In a Servlet 3.0+ environment, you have the option of configuring the Servlet container programmatically as an alternative or in combination with a
The above example is for an application that uses Java-based Spring configuration. If using XML-based Spring configuration, extend directly from
Each filter is added with a default name based on its concrete type and automatically mapped to the
The
Finally, if you need to further customize the
这里只做简要的记录,更详细的使用方式,更详细的资料参见spring参考文档(21.15 Code-based Servlet container initialization),以后会慢慢补充。
21.15 Code-based Servlet container initialization
In a Servlet 3.0+ environment, you have the option of configuring the Servlet container programmatically as an alternative or in combination with a
web.xmlfile. Below is an example of registering a
DispatcherServlet:
import org.springframework.web.WebApplicationInitializer; public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext)); registration.setLoadOnStartup(1); registration.addMapping("/"); } }
WebApplicationInitializeris an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container. An abstract base class implementation of
WebApplicationInitializernamed
AbstractDispatcherServletInitializermakes it even easier to register the
DispatcherServletby simply overriding methods to specify the servlet mapping and the location of the
DispatcherServletconfiguration:
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] { MyWebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
The above example is for an application that uses Java-based Spring configuration. If using XML-based Spring configuration, extend directly from
AbstractDispatcherServletInitializer:
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer { @Override protected WebApplicationContext createRootApplicationContext() { return null; } @Override protected WebApplicationContext createServletApplicationContext() { XmlWebApplicationContext cxt = new XmlWebApplicationContext(); cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); return cxt; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
AbstractDispatcherServletInitializeralso provides a convenient way to add
Filterinstances and have them automatically mapped to the
DispatcherServlet:
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer { // ... @Override protected Filter[] getServletFilters() { return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter() }; } }
Each filter is added with a default name based on its concrete type and automatically mapped to the
DispatcherServlet.
The
isAsyncSupportedprotected method of
AbstractDispatcherServletInitializerprovides a single place to enable async support on the
DispatcherServletand all filters mapped to it. By default this flag is set to
true.
Finally, if you need to further customize the
DispatcherServletitself, you can override the
createDispatcherServletmethod.
相关文章推荐
- Spring的@Autowired用法浅谈
- 安装Eclipse的SVN插件(离线安装 or 在线安装)
- java的文件读写
- Office之PDF
- 关于在生产上查看Java 版本号 java -version 不可用的问题的思考
- 循序渐进看Java web日志跟踪(2)-Java日志API认识
- Spring+EhCache缓存实例(详细讲解+源码下载)
- Spring+EhCache缓存实例(详细讲解+源码下载)
- Spring+EhCache缓存实例(详细讲解+源码下载)
- Spring+EhCache缓存实例(详细讲解+源码下载)
- Java中-classpath和路径的使用
- Spring学习2:bean属性
- Spring学习1:环境搭建
- Java之虚拟机Dalvk原理简介
- java socket SocketException: recvfrom failed: ECONNRESET
- JAVA成员变量初始化
- hdu 2524 java
- Java继承小练习
- Spring MVC 学习笔记 json格式的输入和输出
- Spring Boot JDBC 连接数据库