@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别

1.@EnableWebMvc/WebMvcConfigurationSuppport

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration。而DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport。

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...

2.@EnableWebMvc/@EnableAutoConfiguration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
...
}@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
...
}

@EnableAutoConfiguration是springboot项目的启动类注解@SpringBootApplication的子元素,主要功能为自动配置。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
...
}AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
...
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}Registrar.class})
public @interface AutoConfigurationPackage {
}

@EnableAutoConfiguration实际是导入EnableAutoConfigurationImportSelector和Registar两个类,主要功能是通过SpringFactoriesLoader.loadFactoryNames()导入jar下面配置文件META-INF/spring.factories。

 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

配置文件中的内容如下:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
...
其中有WebMvcAutoConfiguration,WebMvcAutoConfiguration源码如下
@Configuration
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
...
}

@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})意思是如果存在它修饰类的bean,则不需要再创建这个bean。

由此可得出结论:

如果有配置文件继承了DelegatingWebMvcConfiguration,或者WebMvcConfigurationSupport,或者配置文件有@EnableWebMvc,那么@EnableAutoConfiguration中的WebMvcAutoConfiguration将不会被自动配置,而是使用WebMvcConfigurationSupport的配置。

@WebMvcConfigurationAdapter/WebMvcConfigurer

已弃用,最好用implements WebMvcConfigurer代替。

 

总结:

1. implements WebMvcConfigurer:不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置。

2. @EnableWebMvc+implements WebMvcConfigurer:会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置。

3. extends WebMvcConfigurationSupport : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置。

4. extends DelegatingWebMvcConfiguration:会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置。

 

最后欢迎大家访问我的个人网站:1024s​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值