Spring Boot 中的 Dispatcher Servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31981036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:43:13  来源:igfitidea点击:

Dispatcher Servlet in Spring Boot

springspring-boot

提问by user3534483

In my Spring Boot application with packaging type as war, i am configuring Spring MVC. As i understand we dont have to configure Dispatcher Servlet Manually. However, i old style of web.xml i used to configure Dispatcher Servlet and then i used to pass contextClass and contextConfigLocation as follows

在打包类型为 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>      

I belive this was to indicate that SpringMvcConfig (my custom class with spring mvc configuration) is the configuration class for Spring MVC..

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

However, In spring boot if Dispatcher Servlet is configured Automatically, how can i pass my custom class to dispatcher Servlet ?

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

In my Spring Boot application, my SpringMvcConfig class extends from WebMvcConfigurerAdapter and is annotated with @Configuration class

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

Help Needed...

需要帮助...

回答by Shawn.X.Du

Right in the configuration class which is annotated by @Configuration you could define your dispatcherServlet and pass init-parameter to it.

在@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;
}

Another way would be to create a paramter map and then set parameter for registration bean. Thisstream shows how to do it.

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