@EnableWebMvc在做什么?


如红框所示,在构建beanmap的列表过程中,会主动加载该类,构建默认bean列表。在DelegatingWebMvcConfiguration中,会建立以下默认bean列表。
HandlerMapping:
Bean: requestMappingHandlerMapping
Bean: viewControllerHandlerMapping
Bean: beanNameHandlerMapping
Bean: resourceHandlerMapping
Bean: defaultServletHandlerMapping
HandlerAdapter:
Bean: requestMappingHandlerAdapter
Bean: httpRequestHandlerAdapter
Bean: simpleControllerHandlerAdapter
ExceptionResolver
Bean: handlerExceptionResolver
Other:
Bean: mvcConversionService
Bean: mvcValidator
以上bean也是可以通过基于WebMvcConfigurationSupport派生新配置类来进行高级的修改上述bean的配置。

@Configuration
@EnableWebMvc
//@ComponentScan(basePackages = "web.api.module", useDefaultFilters = false, includeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})
//})
@ComponentScan("web.api.module.*")
@Import({ SecurityConfig.class })
public class WebConfig extends WebMvcConfigurationSupport {
	
	private static final Logger logger = Logger.getLogger(WebConfig.class);

	public WebConfig(){

	}

	@Bean
	public ViewResolver viewResolver() {
		logger.info("ViewResolver");
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

    @Bean
    public MessageSource messageSource() {
    	logger.info("MessageSource");
    	ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    	messageSource.setBasename("config.messages.messages");
    	
    	return messageSource;
    }

    @Bean
    public HandlerAdapter servletHandlerAdapter(){
    	logger.info("HandlerAdapter");
    	return new SimpleServletHandlerAdapter();
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
    	logger.info("LocaleChangeInterceptor");
    	return new LocaleChangeInterceptor();
    }

    @Bean(name="localeResolver")
    public CookieLocaleResolver cookieLocaleResolver(){
    	logger.info("CookieLocaleResolver");
    	return new CookieLocaleResolver();
    }

	@Bean
	public CsrfIntercepter initializingCsrfInterceptor(){
		logger.info("CsrfIntercepter");
		return new CsrfIntercepter();
	}
    
    /**                                                          
    * 描述 : <注册自定义拦截器>. 
*

<使用方法说明>

* @return */ @Bean public LoginCheckInterceptor initializingInterceptor(){ logger.info("LoginCheckInterceptor"); return new LoginCheckInterceptor(); } /** * 描述 : .
*

<这个比较奇怪,理论上应该是不需要的>

* @return */ @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { logger.info("RequestMappingHandlerMapping"); return super.requestMappingHandlerMapping(); } @Override protected void addInterceptors(InterceptorRegistry registry) { // TODO Auto-generated method stub logger.info("addInterceptors start"); registry.addInterceptor(localeChangeInterceptor()); registry.addInterceptor(initializingInterceptor()); // registry.addInterceptor(initializingCsrfInterceptor()); logger.info("addInterceptors end"); } @Bean public HandlerMapping resourceHandlerMapping() { logger.info("HandlerMapping"); return super.resourceHandlerMapping(); } /** * 描述 : <资源访问处理器>.
*

<可以在jsp中使用/static/**的方式访问/WEB-INF/static/下的内容>

* @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { logger.info("addResourceHandlers"); registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/"); } /** * 描述 : <异常处理器>.
*

<系统运行时遇到指定的异常将会跳转到指定的页面>

* @return */ @Bean(name="exceptionResolver") public ExceptionResolver simpleMappingExceptionResolver(){ logger.info("ExceptionResolver"); ExceptionResolver simpleMappingExceptionResolver= new ExceptionResolver(); simpleMappingExceptionResolver.setDefaultErrorView("common_error"); simpleMappingExceptionResolver.setExceptionAttribute("exception"); Properties properties = new Properties(); properties.setProperty("java.lang.RuntimeException", "common_error"); simpleMappingExceptionResolver.setExceptionMappings(properties); return simpleMappingExceptionResolver; } /** * 描述 : .
*

<这个比较奇怪,理论上应该是不需要的>

* @return */ @Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { logger.info("RequestMappingHandlerAdapter"); return super.requestMappingHandlerAdapter(); } @Override protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() { logger.info("ConfigurableWebBindingInitializer"); ConfigurableWebBindingInitializer initializer = super.getConfigurableWebBindingInitializer(); return initializer; } }