zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynamic、WebApplicationInitializer)

    一、

    1.如想在DispatcherServlet在Servlet容器中注册后自定义一些操作,如开启文件上传功能,则可重写通过AbstractAnnotationConfigDispatcherServletInitializer 的customizeRegistration() 来实现

     1 //  After  AbstractAnnotation ConfigDispatcherServletInitializer registers  DispatcherServlet with the servlet
     2 //  container, it calls the  customizeRegistration() method, passing in the  Servlet-
     3 //  Registration.Dynamic that resulted from the servlet registration. By overriding
     4 //  customizeRegistration() , you can apply additional configuration to  DispatcherServlet .
     5 //  With the  ServletRegistration.Dynamic that’s given to  customizeRegistration() ,
     6 //  you can do several things, including set the load-on-startup priority by calling  set-
     7 //  LoadOnStartup() , set an initialization parameter by calling  setInitParameter() , and
     8 //  call  setMultipartConfig() to configure Servlet 3.0 multipart support.
     9   @Override
    10   protected void customizeRegistration(Dynamic registration) {
    11     registration.setMultipartConfig(
    12         new MultipartConfigElement("/tmp/spittr/uploads", 2097152, 4194304, 0));
    13   }

    2.通过重定WebApplicationInitializer的onStartup来注册servlet

     1 package com.myapp.config;
     2 import javax.servlet.ServletContext;
     3 import javax.servlet.ServletException;
     4 import javax.servlet.ServletRegistration.Dynamic;
     5 import org.springframework.web.WebApplicationInitializer;
     6 import com.myapp.MyServlet;
     7 public class MyServletInitializer implements WebApplicationInitializer {
     8     @Override
     9     public void onStartup(ServletContext servletContext)
    10     throws ServletException {
    11         Dynamic myServlet =
    12             servletContext.addServlet("myServlet", MyServlet.class);
    13         myServlet.addMapping("/custom/**");
    14     }
    15 }

    is a rather basic servlet-registering initializer class. It registers a servlet and maps it to a single path. You could use this approach to register DispatcherServlet manually. (But there’s no need, because AbstractAnnotationConfigDispatcher-
    ServletInitializer does a fine job without as much code.)

    3.通过重定WebApplicationInitializer的onStartup来注册filter

    1 @Override
    2 public void onStartup(ServletContext servletContext)
    3 throws ServletException {
    4     javax.servlet.FilterRegistration.Dynamic filter = servletContext.addFilter("myFilter", MyFilter.class);
    5     filter.addMappingForUrlPatterns(null, false, "/custom/*");
    6 }

    4.To register one or more filters and map them to DispatcherServlet , all you need to do is override the getServletFilters() method of AbstractAnnotationConfigDispatcherServletInitializer .

    1 @Override
    2 protected Filter[] getServletFilters() {
    3     return new Filter[] { new MyFilter() };
    4 }

    As you can see, this method returns an array of javax.servlet.Filter . Here it only returns a single filter, but it could return as many filters as you need. There’s no need to declare the mapping for the filters; any filter returned from getServletFilters() will automatically be mapped to DispatcherServlet

  • 相关阅读:
    CAP 与数据一致性
    C++的构造函数为何不能为虚函数
    构造函数和析构函数中可以调用调用虚函数吗
    HTTP状态码
    C++ 单例模式实现
    【转】十大经典排序算法
    C++ short/int/long/long long 等数据类型大小
    块/文件/对象三种存储的优缺点
    罗振宇《时间的朋友》2019-2020
    Google Hacking
  • 原文地址:https://www.cnblogs.com/shamgod/p/5244979.html
Copyright © 2011-2022 走看看