本博文基于:springboot2.1.6:
扩展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的自动配置类.
- 在做其他自动配置时会导入 ;@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