WebMvcConfigurer

WebMvcConfigurer

@Configuration + WebMvcConfigurer接口

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

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

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

@Configuration   //定义是spring的bean配置类
public class Mymvcconfig implements WebMvcConfigurer{
	@Override
    public 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{
	@Override
    public 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() {
    }
   ......

你可能感兴趣的:(spring)