Dispatcher Servlet in Spring Boot
Asked Answered
A

2

10

In my Spring Boot application with packaging type as war, I am configuring Spring MVC. As I understand we don't 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

<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..

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

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

Auriscope answered 13/8, 2015 at 6:31 Comment(0)
P
0

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

@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. This stream shows how to do it.

Parfleche answered 2/12, 2015 at 14:3 Comment(1)
You have not answered the question. Your code shows dispatcherServlet() but no indication is given of what this is or how it is defined - which is what the question asks.Markova
D
0

I think you have to create a config class as follow:

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[] { "/" };
    }

}
Desired answered 11/5, 2021 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.