Spring mvc 装配、相关配置
文章目录
- 官方文档地址
- 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
@Configuration
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
//...内容省略
}
如代码,当没有存在装配的WebMvcConfigurationSupport,此配置生效。WebMvcConfigurationSupport中注册了大量WebMvcConfigurer中的配置类,主要用于设置默认配置。
通过静态内部类WebMvcAutoConfigurationAdapter注入配置设置WebMvcConfigurer默认配置(实际上一个接口方法都没重写)
实际上 WebMvcAutoConfiguration中内部类EnableWebMvcConfiguration继承自DelegatingWebMvcConfiguration注解上写着Configuration equivalent to {@code @EnableWebMvc}. 相当于使用EnableWebMvc,这里注册的bean可以作为自己配置的默认配置使用
@EnableWebMvc
作用告诉程序不使用默认配置,使用自己的配置。同时注入DelegatingWebMvcConfiguration作为缺省配置。
源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
当添加EnableWebMvc注解过后,引入DelegatingWebMvcConfiguration作为默认配置bean,该类继承自WebMvcConfigurationSupport。这时WebMvcAutoConfiguration将不再生效,需要注意!那么引入的配置WebMvcProperties.class, ResourceProperties.class也不再生效,具体查看源码注解。
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
//...
}
@Configuration
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter
implements WebMvcConfigurer, ResourceLoaderAware {
//...代码省略
}
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中:
/**
* MVC配置
*
* @author Mark sunlightcs@gmail.com
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private AuthorizationInterceptor authorizationInterceptor;
@Autowired
private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;
/**
* 添加拦截器
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authorizationInterceptor).addPathPatterns("/app/**");
}
/**
* 添加参数解析器
* @param argumentResolvers
*/
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
}
}
具体请查看文档参数配置模块相关内容
异常处理
参考文档Annotated Controllers - Controller Advice章节
异常处理拦截器可以
/**
* 异常拦截器
*
* @author 50238
*/
@ControllerAdvice
@Log4j2
public class AdviceController {
/**
* 拦截业务异常
*
* @return
*/
@ExceptionHandler(BusinessException.class)
@ResponseBody
public Result businessExceptionAdvice(BusinessException exception) {
log.error(exception);
return Result.RESULT_FAILURE(StatusCode.ERROR, exception.getMessage());
}
/**
* 拦截普通异常
*
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public Result exceptionAdvice(Exception exception) {
log.error(exception);
return Result.RESULT_FAILURE(StatusCode.ERROR, exception.getMessage());
}
}
标记方法@ResponseStatus(value = HttpStatus.xxx)可以使用httpcode返回错误
总结
Spring mvc 的配置主要通过实现WebMvcConfigurer接口设置相关参数,之前不熟悉每次都需要百度百度,去没理解到具体类的使用,这次算是专门花时间看了源码。
还没有评论,来说两句吧...