33. Silicon Valley ﹣ springboot ﹣ web development - expand and take over SpringMVC in an all-round way

2. Expand spring MVC

This was originally done in spring-mvc.xml

    <mvc:view-controller path="/hello" view-name="success"/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hello"/>
            <bean></bean>
        </mvc:interceptor>
    </mvc:interceptors>

Write a Configuration class (@ Configuration), which is of type WebMvcConfigurerAdapter; cannot label @ EnableWebMvc;

Not only keep all the automatic configuration, but also use our extended configuration;

//Use WebMvcConfigurerAdapter to extend the function of spring MVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //Browser sends / atguigu request to success
        registry.addViewController("/atguigu").setViewName("success");
    }
}

Principle:

1) webmvcauautoconfiguration is the automatic configuration class of spring MVC

2) it will be imported during other automatic configuration; @ Import(EnableWebMvcConfiguration.class)

    @Configuration
	public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
      private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

	 //Get all webmvcconfigures from the container
      @Autowired(required = false)
      public void setConfigurers(List<WebMvcConfigurer> configurers) {
          if (!CollectionUtils.isEmpty(configurers)) {
              this.configurers.addWebMvcConfigurers(configurers);
            	//A reference implementation that calls all WebMvcConfigurer related configurations together;  
            	@Override
             // public void addViewControllers(ViewControllerRegistry registry) {
              //    for (WebMvcConfigurer delegate : this.delegates) {
               //       delegate.addViewControllers(registry);
               //   }
              }
          }
	}

3) all webmvcconfigures in the container will work together;

Our configuration class will also be called.

Effect: both the automatic configuration of spring MVC and our extension configuration will work.

3. Take over spring MVC in an all-round way;

Spring boot doesn't need to configure spring MVC automatically. It's all our own configuration. All the automatic configurations of spring MVC fail.

We need to add @ EnableWebMvc to the configuration class.
When we use @ EnableWebMvc, static resources cannot be accessed

//Use WebMvcConfigurerAdapter to extend the function of spring MVC
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //Browser sends / atguigu request to success
        registry.addViewController("/atguigu").setViewName("success");
    }
}

Principle:

Why does @ EnableWebMvc auto configuration fail?

1) core of @ EnableWebMvc

@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {

2),

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3),

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
		WebMvcConfigurerAdapter.class })
//This autoconfig class takes effect only when there is no such component in the container
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4) @ EnableWebMvc imports the WebMvcConfigurationSupport component in;

5) the imported WebMvcConfigurationSupport is only the most basic function of spring MVC;

Tags: Spring xml

Posted on Fri, 01 Nov 2019 14:18:34 -0700 by PAFTprod