WebMvcConfigurer

@Configuration + WebMvcConfigurer接口

在spring中我们可以通过用java代码配置bean,不使用xml写bean,改成使用java代码来实现javabean的配置。如下:

@Configuration   //定义是spring的bean配置类
@ComponentScan("com.ex.test")  //扫描包
@Import(other.class)  //导入config
public class beanconfig{@Beanpublic User user(){return new User();}
}

而WebMvcConfigurer配置类其实是Spring内部的一种配置方式,可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter等等的东西对springmvc框架进行配置。如下:

@Configuration   //定义是spring的bean配置类
public class Mymvcconfig implements WebMvcConfigurer{@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");}
}

WebMvcConfigurer接口

public interface WebMvcConfigurer {void configurePathMatch(PathMatchConfigurer var1);void configureContentNegotiation(ContentNegotiationConfigurer var1);void configureAsyncSupport(AsyncSupportConfigurer var1);void configureDefaultServletHandling(DefaultServletHandlerConfigurer var1);void addFormatters(FormatterRegistry var1);void addInterceptors(InterceptorRegistry var1);void addResourceHandlers(ResourceHandlerRegistry var1);void addCorsMappings(CorsRegistry var1);void addViewControllers(ViewControllerRegistry var1);void configureViewResolvers(ViewResolverRegistry var1);void addArgumentResolvers(List<HandlerMethodArgumentResolver> var1);void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> var1);void configureMessageConverters(List<HttpMessageConverter<?>> var1);void extendMessageConverters(List<HttpMessageConverter<?>> var1);void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);Validator getValidator();MessageCodesResolver getMessageCodesResolver();
}

addViewControllers添加一个视图解析器

通过实现addViewControllers向mvc添加一个视图解析器(添加一个页面跳转)

@Configuration   //定义是spring的bean配置类
public class Mymvcconfig implements WebMvcConfigurer{@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/toLogin").setViewName("login");}
}

@EnableWebMvc

@EnableWebMvc是使用Java 注解快捷配置Spring Webmvc的一个注解。在EnableWebMvc导入了一个DelegatingWebMvcConfiguration类,而DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport。

而WebMvcAutoConfiguration类的注解@ConditionalOnMissingBean({WebMvcConfigurationSupport.class}),定义了WebMvcAutoConfiguration只有没有WebMvcConfigurationSupport.class时WebMvcAutoConfiguration才生效,所以有以下结论:

@EnableWebMvc注解的配置类将会全面接管MVC的配置。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {}//-----------------DelegatingWebMvcConfiguration-----------------
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();public DelegatingWebMvcConfiguration() {}.....//-----------------WebMvcAutoConfiguration-----------------
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {public static final String DEFAULT_PREFIX = "";public static final String DEFAULT_SUFFIX = "";private static final String[] SERVLET_LOCATIONS = new String[]{"/"};public WebMvcAutoConfiguration() {}......

WebMvcConfigurer相关推荐

  1. 继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错? springboot 两种方式稳定解决跨域问题

    继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错???springboot 两种方式稳定解决跨域问题! 之前我写了一篇文章,来解决CORS报错 ...

  2. 十一、springboot WebMvcConfigurer与HandlerInterceptorAdapter使用

    springboot WebMvcConfigurer与HandlerInterceptorAdapter使用 简介 WebMvcConfigurer:拦截器的注册类 HandlerIntercept ...

  3. Spring boot 梳理 - WebMvcConfigurer接口 使用案例

    转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,Vi ...

  4. 8、SpringBoot-CRUD默认访问的首页以及thyleaf的静态文件引入/WebMvcConfigurer / WebMvcConfigurationSupport...

    1.导入资源 2.默认的访问首页 (1).将代码写在controller中 @RequestMapping({"/","index.html"}) public ...

  5. SpringBoot 使用WebMvcConfigurer处理请求

    1. 简介 WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制. 基于java-based方式的 ...

  6. SpringBoot中通过重写WebMvcConfigurer的方法配置静态资源映射实现图片上传后返回网络Url

    场景 前端调用上传照片的功能,将某照片上传到服务器上某磁盘路径下,然后将通过静态资源映射,将在服务器上 访问的地址存储到数据库中,这样在需要获取这种照片的时候就能通过服务器上的url来获取和显示这张照 ...

  7. SpringBoot中通过重写WebMvcConfigurer的addCorsMapping方法实现后台服务解决跨域问题

    场景 之所以会跨域,是因为受到了同源策略的限制,同源策略要求源相同才能正常进行通信,即协议.域名.端口号都完全一致. 浏览器出于安全的考虑,使用 XMLHttpRequest对象发起 HTTP请求时必 ...

  8. 实现WebMvcConfigurer接口扩展Spring MVC的功能

    前言: 先查看WebMvcConfigurer接口中都定义了哪些内容 public interface WebMvcConfigurer {default void configurePathMatc ...

  9. webmvcconfigurer配置跨域_为什么加了 Spring Security 会导致 Spring Boot 跨域失效呢?...

    点击上方 IT牧场 ,选择 置顶或者星标 技术干货每日送达 作者:欧阳我去 链接:https://segmentfault.com/a/1190000019485883 作为一个后端开发,我们经常遇到 ...

最新文章

  1. Mongodb最佳实践及使用问题
  2. fiddler filters 使用(fiddler只显示指定请求,fiddler不显示指定请求,即filter请求过滤)转自:http://blog.csdn.net/notejs/article/
  3. angular中的href=unsafe:我该怎么摆脱你的溺爱!!
  4. 大端BigEndian、小端LittleEndian与字符集编码
  5. 【图论】Bellman_Ford算法求有步数限制的最短路(图文详解)
  6. Android的Intent系统调用
  7. 【linux】Redhat 7 更新 yum源
  8. 软件系统测试用例设计,软件系统测试用例设计步骤
  9. python如何解压zip文件_Python压缩解压zip文件
  10. 手机浏览器跳微信小程序
  11. ATR-CKN算法的研究与实现
  12. 没有对象怎么面向对象编程呢?真让人头秃!
  13. JSP基础之网站用户管理
  14. html语法在线检测,HTML语法检测
  15. Java锁与线程的那些“不可描述”的事儿
  16. 测试公总号加入微信开放平台
  17. Windows 7 如何开启休眠
  18. 微信 拒绝共享位置服务器,微信共享位置结束不了怎么办?如何解决问题?
  19. [Albert 朗读行动记录贴]采纳Scalers方法:口语成长行动
  20. QT中 :-1: error: Failed to resolve include /moc_predefs.h“ for moc file

热门文章

  1. linux文件管理和 对bash的理解
  2. tcpdump -i eth0 -n -vvv src or dst port 443
  3. 在bash脚本中进行浮点运算
  4. 工作流表单自定义功能的误区
  5. maven里如何根据不同的environment打包
  6. javaweb学习总结(四十三)——Filter高级开发
  7. 在ASP.Net和IIS中删除不必要的HTTP响应头
  8. 解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的方法...
  9. JavaScript prototype 属性
  10. Django 多数据库联用(看着不错还有源码可以下载)