Dispatch Servlet in Spring Download

In my Spring boot application with packaging type as a war, I configure Spring MVC. As I understand it, we do not need to configure the Servlet dispatcher manually. However, the old web.xml style that I used to configure the Servlet dispatcher, 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 believe this meant that SpringMvcConfig (my custom class with Spring mvc configuration) is the configuration class for Spring MVC ..

However, in Spring boot, if the Servlet dispatcher is configured automatically, how can I pass my own class to the Servlet dispatcher?

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

Help required ...

+8
source share
2 answers

If your configuration class is in the same package or subpackages of the main class (which is annotated using @SpringBootApplication ), you do not need to do anything. @SpringBootApplication scans all package components and all subpackages.

However, if you want any other scanned packages, you can use @ComponentScan . Example:

 @Configuration @EnableAutoConfiguration @ComponentScan(basePackageClasses = {SpringSampleAppApplication.class, LemonConfig.class}) public class SpringSampleAppApplication { public static void main(String[] args) { SpringApplication.run(SpringSampleAppApplication.class, args); } } 

See how @SpringBootApplication is replaced with three annotations with custom @ComponentScan .

There are also other ways to import configuration files, but I find these simplest simplest in common scenarios.

0
source

Right in the configuration class annotated by @Configuration, you can define yourServlet dispatcher and pass it an 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 is to create a parameter map and then set the parameter to register the bean. This thread shows how to do this.

0
source

All Articles