在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

spring与servlet3.0无web.xml文件时的配置方式

2017-2-7 13:39| 发布者: zhangjf| 查看: 434| 评论: 0

摘要: 这里只做简要的记录,更详细的使用方式,更详细的资料参见spring参考文档(21.15 Code-based Servlet container initialization),以后会慢慢补充。 21.15 Code-based Servlet container initialization In a Se ...


这里只做简要的记录,更详细的使用方式,更详细的资料参见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.xml file. Below is an example of registering a DispatcherServlet:

  1. import org.springframework.web.WebApplicationInitializer;
  2. public class MyWebApplicationInitializer implements WebApplicationInitializer {
  3. @Override
  4. public void onStartup(ServletContext container) {
  5. XmlWebApplicationContext appContext = new XmlWebApplicationContext();
  6. appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
  7. ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
  8. registration.setLoadOnStartup(1);
  9. registration.addMapping("/");
  10. }
  11. }
复制代码

WebApplicationInitializer is 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 WebApplicationInitializer named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration:

  1. public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  2. @Override
  3. protected Class<?>[] getRootConfigClasses() { return null;
  4. } @Override
  5. protected Class<?>[] getServletConfigClasses() { return new Class[] { MyWebConfig.class };
  6. } @Override
  7. protected String[] getServletMappings() { return new String[] { "/" };
  8. }
  9. }
复制代码

The above example is for an application that uses Java-based Spring configuration. If using XML-based Spring configuration, extend directly from AbstractDispatcherServletInitializer:

  1. public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
  2. @Override
  3. protected WebApplicationContext createRootApplicationContext() {
  4. return null;
  5. }
  6. @Override
  7. protected WebApplicationContext createServletApplicationContext() {
  8. XmlWebApplicationContext cxt = new XmlWebApplicationContext();
  9. cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
  10. return cxt;
  11. }
  12. @Override
  13. protected String[] getServletMappings() {
  14. return new String[] { "/" };
  15. }
  16. }
复制代码

AbstractDispatcherServletInitializer also provides a convenient way to add Filter instances and have them automatically mapped to the DispatcherServlet:

  1. public class MyWebAppInitializer extends AbstractDispatcherServletInitializer { // ... @Override
  2. protected Filter[] getServletFilters() {
  3. return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
  4. }
  5. }
复制代码

Each filter is added with a default name based on its concrete type and automatically mapped to the DispatcherServlet.

The isAsyncSupported protected method of AbstractDispatcherServletInitializer provides a single place to enable async support on the DispatcherServlet and all filters mapped to it. By default this flag is set to true.

Finally, if you need to further customize the DispatcherServlet itself, you can override the createDispatcherServlet method.

来自: http://my.oschina.net/u/1011578/blog/601719

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 07:55

Copyright 2015-2025 djqfx

返回顶部