Spring mvc 装配、相关配置

小灰灰 2024-04-17 05:54 113阅读 0赞

文章目录

  • 官方文档地址
  • Spring boot 自动装配
    • @EnableWebMvc
  • 相关配置
    • 主要配置
    • 异常处理
  • 总结

官方文档地址

Spring mvc官方文档

Spring boot 自动装配

Spring boot 自动装配通过扫描autoconfig包下的\META-INF\spring.factories进行装配。(具体机制查看文档装配内容)

Spring mvc 通过org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration进行自动装配

其中主要的类需要注意WebMvcConfigurationSupport.class

  1. @Configuration
  2. @ConditionalOnWebApplication(
  3. type = Type.SERVLET
  4. )
  5. @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
  6. @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
  7. @AutoConfigureOrder(-2147483638)
  8. @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
  9. public class WebMvcAutoConfiguration {
  10. //...内容省略
  11. }

如代码,当没有存在装配的WebMvcConfigurationSupport,此配置生效。WebMvcConfigurationSupport中注册了大量WebMvcConfigurer中的配置类,主要用于设置默认配置。

通过静态内部类WebMvcAutoConfigurationAdapter注入配置设置WebMvcConfigurer默认配置(实际上一个接口方法都没重写)

实际上 WebMvcAutoConfiguration中内部类EnableWebMvcConfiguration继承自DelegatingWebMvcConfiguration注解上写着Configuration equivalent to {@code @EnableWebMvc}. 相当于使用EnableWebMvc,这里注册的bean可以作为自己配置的默认配置使用

@EnableWebMvc

作用告诉程序不使用默认配置,使用自己的配置。同时注入DelegatingWebMvcConfiguration作为缺省配置。

源码

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.TYPE})
  3. @Documented
  4. @Import({DelegatingWebMvcConfiguration.class})
  5. public @interface EnableWebMvc {
  6. }

当添加EnableWebMvc注解过后,引入DelegatingWebMvcConfiguration作为默认配置bean,该类继承自WebMvcConfigurationSupport。这时WebMvcAutoConfiguration将不再生效,需要注意!那么引入的配置WebMvcProperties.class, ResourceProperties.class也不再生效,具体查看源码注解。

  1. @Configuration
  2. public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
  3. //...
  4. }
  5. @Configuration
  6. @Import(EnableWebMvcConfiguration.class)
  7. @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
  8. @Order(0)
  9. public static class WebMvcAutoConfigurationAdapter
  10. implements WebMvcConfigurer, ResourceLoaderAware {
  11. //...代码省略
  12. }

Spring boot配置mvc配置生效问题

WebMvcConfigurationSupport和WebMvcConfigurer

Spring MVC 代理配置类 DelegatingWebMvcConfiguration

相关配置

主要配置

org.springframework.web.servlet.config.annotation.WebMvcConfigurer

如拦截器:自定义实现类实现org.springframework.web.servlet.handler.HandlerInterceptorAdapter,通过WebMvcConfigurer注入其中

通过实现WebMvcConfigurer接口,添加mvc配置如renren-fast中:

  1. /**
  2. * MVC配置
  3. *
  4. * @author Mark sunlightcs@gmail.com
  5. */
  6. @Configuration
  7. public class WebMvcConfig implements WebMvcConfigurer {
  8. @Autowired
  9. private AuthorizationInterceptor authorizationInterceptor;
  10. @Autowired
  11. private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;
  12. /**
  13. * 添加拦截器
  14. *
  15. * @param registry
  16. */
  17. @Override
  18. public void addInterceptors(InterceptorRegistry registry) {
  19. registry.addInterceptor(authorizationInterceptor).addPathPatterns("/app/**");
  20. }
  21. /**
  22. * 添加参数解析器
  23. * @param argumentResolvers
  24. */
  25. @Override
  26. public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  27. argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
  28. }
  29. }

具体请查看文档参数配置模块相关内容

异常处理

参考文档Annotated Controllers - Controller Advice章节

异常处理拦截器可以

  1. /**
  2. * 异常拦截器
  3. *
  4. * @author 50238
  5. */
  6. @ControllerAdvice
  7. @Log4j2
  8. public class AdviceController {
  9. /**
  10. * 拦截业务异常
  11. *
  12. * @return
  13. */
  14. @ExceptionHandler(BusinessException.class)
  15. @ResponseBody
  16. public Result businessExceptionAdvice(BusinessException exception) {
  17. log.error(exception);
  18. return Result.RESULT_FAILURE(StatusCode.ERROR, exception.getMessage());
  19. }
  20. /**
  21. * 拦截普通异常
  22. *
  23. * @return
  24. */
  25. @ExceptionHandler(Exception.class)
  26. @ResponseBody
  27. public Result exceptionAdvice(Exception exception) {
  28. log.error(exception);
  29. return Result.RESULT_FAILURE(StatusCode.ERROR, exception.getMessage());
  30. }
  31. }

标记方法@ResponseStatus(value = HttpStatus.xxx)可以使用httpcode返回错误

总结

Spring mvc 的配置主要通过实现WebMvcConfigurer接口设置相关参数,之前不熟悉每次都需要百度百度,去没理解到具体类的使用,这次算是专门花时间看了源码。

发表评论

表情:
评论列表 (有 0 条评论,113人围观)

还没有评论,来说两句吧...

相关阅读