【问题标题】:Dispatcher Servlet in Spring BootSpring Boot 中的调度程序 Servlet
【发布时间】:2015-11-05 23:54:51
【问题描述】:

在包装类型为 war 的 Spring Boot 应用程序中,我正在配置 Spring MVC。据我了解,我们不必手动配置 Dispatcher Servlet。但是,我使用旧样式的 web.xml 来配置 Dispatcher Servlet,然后我用来传递 contextClass 和 contextConfigLocation 如下

<servlet>
    <description>
    </description>
    <display-name>DispatcherServlet</display-name>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <description>contextClass</description>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <description>contextConfigLocation</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.xxx.yyy.jdorderspringmvcweb.config.SpringMvcConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>      

我相信这是为了表明 SpringMvcConfig(我的带有 spring mvc 配置的自定义类)是 Spring MVC 的配置类..

但是,在 Spring Boot 中,如果 Dispatcher Servlet 是自动配置的,我如何将我的自定义类传递给 dispatcher Servlet?

在我的 Spring Boot 应用程序中,我的 SpringMvcConfig 类从 WebMvcConfigurerAdapter 扩展而来,并使用 @Configuration 类进行注释

需要帮助...

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    我认为你必须创建一个配置类如下:

    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[] { DemoAppConfig.class };
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      在由@Configuration 注释的配置类中,您可以定义您的dispatcherServlet 并将init-parameter 传递给它。

      @Bean
      public ServletRegistrationBean dispatcherServletRegistration() {
          ServletRegistrationBean registrationBean = new ServletRegistrationBean(dispatcherServlet());
          registrationBean.addInitParameter("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext");  
          registrationBean.addInitParameter("contextConfigLocation","com.xxx.yyy.jdorderspringmvcweb.config.SpringMvcConfig");
          return registrationBean;
      }
      

      另一种方法是创建一个参数映射,然后为注册 bean 设置参数。 This 流显示了如何做到这一点。

      【讨论】:

      • 您还没有回答问题。您的代码显示dispatcherServlet(),但没有说明这是什么或它是如何定义的——这就是问题所在。
      猜你喜欢
      • 1970-01-01
      • 2018-01-30
      • 2015-05-19
      • 1970-01-01
      • 2015-10-04
      • 2017-01-05
      • 2018-04-11
      • 2016-08-02
      • 2015-09-10
      相关资源
      最近更新 更多