【SpringBoot】如何擴展與全面接管springmvc

本博文基於:springboot2.1.6:

springboot關於springmvc官方文檔

擴展springmvc

通過上一篇博客的敘述得知 ,springmvc默認的幫我們配置了一些基本功能 ,但是默認的配置 ,對於開發一個完善的系統來說 , 是遠遠不夠的 …

舉個例子…如果要像springmvc在配置文件中實現 路徑映射, 和攔截器等等功能 …boot該如何去做?

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

讓我們去spring官方文檔看看:

If you want to keep Spring Boot MVC features and you want to add additional
MVC configuration (interceptors, formatters, view
controllers, and other features), you can add your own @Configuration class of type
WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom
instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or
ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter
instance to provide such components.

文檔的擴展發法是: 必須標註註解:@Configuration與必須爲WebMvcConfigurer類型,但是不能標註註解@EnableWebMvc.

編寫一個配置類 (@Configuration ) 實現WebMvcConfigurer 但是必須 沒有@EnableWebMvc

public class MyConfig  implements WebMvcConfigurer {
    //WebMvcConfigurer 用來進行springmvc的擴展功能
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
             //將請求進行轉發
         registry.addViewController("/fsl").setViewName("home");
    }
}

原理:

1)WebMvcAutoConfiguration是springboot的自動配置類.

  1. 在做其他自動配置時會導入 ;@Import(EnableWebMvcConfiguration.class)

WebMvcAutoConfiguration - > WebMvcAutoConfigurationAdapter:

@Configuration
    @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
    @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {
查看:EnableWebMvcConfiguration方法
    @Configuration
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
        

觀察DelegatingWebMvcConfiguration ,發現其中有一個方法是:


	@Autowired(required = false)
	public void setConfigurers(List<WebMvcConfigurer> configurers) {
		if (!CollectionUtils.isEmpty(configurers)) {
			this.configurers.addWebMvcConfigurers(configurers);
		}
	}

代表注入容器中所有的configurer.


    public void addWebMvcConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.delegates.addAll(configurers);
        }

    }
打開
this.configurers.addWebMvcConfigurers(configurers);
容器中所有的WebMvcConfigurer都會一起起作用;

其中包括我們自己自定義的configurer

全面接管springmvc

If you want to take complete control of Spring MVC, you can add your own @Configuration
annotated with @EnableWebMvc.

解釋是:只要在自定義配置類中標註註解@EnableWebMvc,springboot中關於springmvc的所有配置都會失效,而只會利用我們自己的自定義配置.

測試:

//@EnableWebMvc
@Configuration
public class MyConfig  implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
         registry.addViewController("/fds").setViewName("home");
    }
}

http://localhost:8080/favicon.ico能夠正常的訪問的到圖片.

將@EnableWebMvc加上之後 , 返回的結果如下:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

There was an unexpected error (type=Not Found, status=404).
No message available

原理:

自動配置失效原理:

@EnableWebMvc:\


@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class}) //核心
public @interface EnableWebMvc {
}

DelegatingWebMvcConfiguration.class:


@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

WebMvcAutoConfiguration:

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) //沒有相關功能的時候 ,纔會有這種 
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

@EnableWebMvc將WebMvcConfigurationSupport 組件導入進來 ,導入的只是springmvc的基本 功能,導入之後WebMvcAutoConfiguration由於不滿足註解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) 所以 ,這個自動配置類中的所有bean都不能被自動配置 .

總結:

2)、在SpringBoot中會有非常多的xxxConfigurer幫助我們進行擴展配置
3)、在SpringBoot中會有很多的xxxCustomizer幫助我們進行定製配置
參考: https://jingyan.baidu.com/article/a378c960e1b3e3b3282830fa.html
https://blog.csdn.net/Java_Glory/article/details/89839891
http://www.mamicode.com/info-detail-2507373.html
https://www.cnblogs.com/fanqisoft/p/10324465.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章