AbstractAnnotationConfigDispatcherServletlnitializer
在 Servlet3.0 环境中,容器会在类路径中查找实现javax.servlet.ServletContainerlnitializer
接口的类,如果找到的话就用它来配置 Servlet 容器
Spring 提供了这个接口的实现,名为SpringServletContainerlnitializer
,这个类反过来又会查找实现WebApplicationlnitializer
的类并将配置的任务交给它们来完成
Spring3.2 引入了一个便利的 WebApplicationlnitializer
基础实现,名为AbstractAnnotationConfigDispatcherServletlnitializer
当类扩展了AbstractAnnotationConfigDispatcherServletlnitializer
并将其部署到 Servlet3.0 容器时,容器会自动发现它,并用它来配置Servlet 上下文
初始化类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; }
@Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; }
@Override protected String[] getServletMappings() { return new String[]{"/"}; }
@Override protected Filter[] getServletFilters() { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceResponseEncoding(true); HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter(); return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter}; } }
|
SpringMVC 配置类
SpringMVC 配置类清单
- 1、扫描组件
- 2、视图解析器
- 3、view-controller
- 4、default-servlet-handler
- 5、MVC注解驱动
- 6、文件上传解析器
- 7、拦截器
- 8、异常处理解析器
1 2 3 4 5 6 7
| @Configuration
@ComponentScan("com.vectorx.springmvc.controller")
@EnableWebMvc public class WebConfig {}
|
视图解析器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| @Configuration @ComponentScan("com.vectorx.springmvc.controller") @EnableWebMvc public class WebConfig {
@Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCharacterEncoding("UTF-8"); return templateResolver; }
@Bean public ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; }
@Bean public ViewResolver viewResolver(ISpringTemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setOrder(1); viewResolver.setCharacterEncoding("UTF-8"); viewResolver.setTemplateEngine(templateEngine); return viewResolver; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @Configuration @ComponentScan("com.vectorx.springmvc.controller") @EnableWebMvc public class WebConfig implements WebMvcConfigurer {
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**"); }
@Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver(); Properties properties = new Properties(); properties.setProperty("java.lang.ArithmeticException", "error"); exceptionResolver.setExceptionMappings(properties); exceptionResolver.setExceptionAttribute("ex"); resolvers.add(exceptionResolver); } }
|
文件上传解析器
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Configuration @ComponentScan("com.vectorx.springmvc.controller") @EnableWebMvc public class WebConfig implements WebMvcConfigurer {
@Bean public MultipartResolver multipartResolver() { MultipartResolver multipartResolver = new CommonsMultipartResolver(); return multipartResolver; } }
|
测试
访问首页
测试异常
附录:SpringMVC 配置类总览
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| @Configuration
@ComponentScan("com.vectorx.springmvc.controller")
@EnableWebMvc public class WebConfig implements WebMvcConfigurer {
@Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCharacterEncoding("UTF-8"); return templateResolver; }
@Bean public ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; }
@Bean public ViewResolver viewResolver(ISpringTemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setOrder(1); viewResolver.setCharacterEncoding("UTF-8"); viewResolver.setTemplateEngine(templateEngine); return viewResolver; }
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); }
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
@Bean public MultipartResolver multipartResolver() { MultipartResolver multipartResolver = new CommonsMultipartResolver(); return multipartResolver; }
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**"); }
@Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver(); Properties properties = new Properties(); properties.setProperty(ArithmeticException.class.getCanonicalName(), "error"); exceptionResolver.setExceptionMappings(properties); exceptionResolver.setExceptionAttribute("ex"); resolvers.add(exceptionResolver); } }
|
总结
本届重点掌握
- web 工程初始化的配置方法
- SpringMVC 配置类的组件配置方法
附上导图,仅供参考