为什么此初始化和配置的Dispatcher Servlet不处理任何请求?

时间:2019-07-14 13:31:38

标签: java spring spring-mvc servlets servlet-3.0

我正在使用一个需要使用该库在应用程序的Web应用程序上下文中注册其控制器bean的库。这样应用程序就可以访问库的控制器和配置Bean。

该应用程序已在使用servlet初始化程序:(我无法修改该应用程序的源代码)

@Order(1)
public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /** Constructor */
    public SpringServletInitializer() {
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringCoreConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringWebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        return new Filter[]{characterEncodingFilter};
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("spring.profiles.default", "ADMIN_PROFILE");
        super.onStartup(servletContext);
    }

}

我尝试通过扩展DispatcherServlet或实现AbstractAnnotationConfigDispatcherServletInitializer来创建新的WebApplicationInitializer。但是,无论哪种方式,都不会调用新的DispatcherServlet,即使它已成功初始化和配置。该应用程序仍然使用自己的调度程序servlet,而不是同时使用两者。

public class CustomConfJavaInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(CustomApiConfiguration.class);
        ctx.setServletContext(servletContext);

        ServletRegistration.Dynamic servlet = servletContext.addServlet(
                "dispatcher2", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/custom");
    }
}


要么

public class CustomConfJavaInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("spring.profiles.default", "ADMIN_PROFILE");
        super.onStartup(servletContext);
    }

      @Override
      protected Class<?>[] getRootConfigClasses() {
        return null;
      }

      @Override
      protected Class<?>[] getServletConfigClasses() {
        return new Class[]{CustomApiConfiguration.class};
      }


      @Override
      protected String getServletName() {
        return "dispatcher2";
      }


    @Override
    protected String[] getServletMappings() {
        return new String[]{"/", "/custom", "/custom/*"};
    }   

}

我希望只修改默认的调度程序servlet并添加我的配置,但是我不确定该怎么做?

org.springframework.web.servlet.DispatcherServlet|DispatcherServlet with name 'dispatcher' processing GET request for [/app-back/custom/ping]
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping|Matching patterns for request [/custom/ping] are [/**]
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping|URI Template variables for request [/custom/ping] are {}

但是,尽管如此,由于允许我们有多个Dispatcher Servlet,为什么从未调用过“ dispatcher2”?
我尝试扩展ServletInitializer,因此首先调用了我的班级,它似乎确实起作用。每个单独的请求都会调用我的dispatcher2,而不是其Dispatcher Servlet dispatcher会调用我的value。这是否意味着创建的第一个调度程序是唯一的调度程序?

1 个答案:

答案 0 :(得分:0)

您的第二个调度员应实施WebApplicationInitializer

public class CustomConfJavaInitializer 
   implements WebApplicationInitializer {