Spring boot 梳理 [email protected]、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

  1. @EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport

    1. 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration  

      1. @Retention(RetentionPolicy.RUNTIME)
        @Target({ElementType.TYPE})
        @Documented
        @Import({DelegatingWebMvcConfiguration.class})
        public @interface EnableWebMvc {
        }
      2. DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport
        1. @Configuration
          public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
          ...
    2. @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter使用

      1. WebMvcConfigurationAdapter已经废弃,最好用implements WebMvcConfigurer代替

        @Configuration
        public class MyConfig implements WebMvcConfigurer {
        
        }

        如果使用继承,WebMvcConfigurationSupport,DelegatingWebMvcConfiguration,或者使用@EnableWebMvc,

        需要注意会覆盖application.properties中关于WebMvcAutoConfiguration的设置,需要在自定义配置中实现,如

        springboot2.0、spring5.0 拦截器配置WebMvcConfigurerAdapter过时使用WebMvcConfigurationSupport来代替 新坑

        示例如下

        Configuration
        @EnableWebMvc
        public class MyConfig implements WebMvcConfigurer {
        
        }
        @Configuration
        public class MyConfig extends WebMvcConfigurationSupport {
        
        }
        @Configuration
        public class MyConfig extends DelegatingWebMvcConfiguration {
        
        }上面代码中需要在类中实现关于WebMvcAutoConfiguration的配置,而不是在application.properties中。
      2. 总结
        
        implements WebMvcConfigurer : 不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置@EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置extends WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置extends DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
  2.   @EnableAutoConfiguration
    1. @EnableAutoConfiguration是springboot项目的启动类注解@SpringBootApplication的子元素,主要功能为自动配置
    2. @EnableAutoConfiguration实际是导入了EnableAutoConfigurationImportSelector和Registrar两个类
    3. @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Inherited
      @AutoConfigurationPackage
      @Import({AutoConfigurationImportSelector.class})
      public @interface EnableAutoConfiguration {
      ...
      }
      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Inherited
      @Import({Registrar.class})
      public @interface AutoConfigurationPackage {
      }
    4. 这两个类的具体原理有些复杂,不太清除,主要内容是通过SpringFactoriesLoader.loadFactoryNames()导入jar下面的配置文件META-INF/spring.factories
    5. 配置文件中的内容如下
      1. # 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,\
    6.   其中有WebMvcAutoConfiguration,WebMvcAutoConfiguration源码如下
      1. @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 {
        ...
        }
      2. @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})意思是如果存在它修饰的类的bean
        ,则不需要再创建这个bean。
      3. 由此可得出结论:
        如果有配置文件继承了DelegatingWebMvcConfiguration,
        或者WebMvcConfigurationSupport,或者配置文件有@EnableWebMvc,那么 @EnableAutoConfiguration 中的
        WebMvcAutoConfiguration 将不会被自动配置,而是使用WebMvcConfigurationSupport的配置。
  3. @SpringBootApplication
    1. @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 {
      ...
      }
  4. 转:https://www.cnblogs.com/sufferingStriver/p/9026764.html

原文地址:https://www.cnblogs.com/jiangtao1218/p/10241551.html

时间: 2024-07-31 01:19:05

Spring boot 梳理 [email protected]、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)的相关文章

@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别

@EnableWebMvc是什么 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration. @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({DelegatingWebMvcConfiguration.class}) public @interface EnableWebMvc { } DelegatingWebMvcCo

(转)spring boot注解 [email protected] 异步调用

原文:http://www.cnblogs.com/azhqiang/p/5609615.html EnableAsync注解的意思是可以异步执行,就是开启多线程的意思.可以标注在方法.类上. 1 @Component 2 public class Task { 3 4 @Async 5 public void doTaskOne() throws Exception { 6 // 同上内容,省略 7 } 8 9 @Async 10 public void doTaskTwo() throws

Spring高级话题[email protected]***注解的工作原理

出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy注解 激活Aspect自动代理 <aop:aspectj-autoproxy/> 开启对AspectJ自动代理的支持. 在用到AOP的自动代理的时候用,如果你理解了Java的动态代理,很容易的就会熟悉AOP的自动代理的. @EnableAsync @EnableAsync注解开启异步方法的支持. 这个相信大家都比较熟悉的.对于异步

Spring boot 梳理 - Spring Boot 属性配置和使用(转)

转:https://blog.csdn.net/isea533/article/details/50281151 Spring Boot 支持多种外部配置方式,这些方式优先级如下: 命令行参数 来自java:comp/env的JNDI属性 Java系统属性(System.getProperties()) 操作系统环境变量 RandomValuePropertySource配置的random.*属性值 jar包外部的application-{profile}.properties或applicat

Spring boot 梳理 - 代码结构(Main类的位置)

Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootApplication 可能会有问题. Main类在包路径中的位置:强烈建议main类放在包的根路径上.We generally recommend that you locate your main application class in a root package above other classe

Spring boot 梳理 - WebMvcConfigurer接口 使用案例

转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,ViewResolver,MessageConverter,该怎么做呢.在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等.SpringBoot 2.0 后,该类被标记为@Deprecated.因此我们只能靠实现WebMvc

Spring boot 梳理 - SpringApplication

简单启动方式 public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, args); } 调试方式启动 java -jar myproject-0.0.1-SNAPSHOT.jar --debug 高级启动方式 @SpringBootApplication public class App { public static void main( String[] args

Spring学习记录[email&#160;protected] Propagation

起因 学习Spring的时候就知道aop有一个应用是声明式注解..反正往Service上一丢@Transactional就完事了..不用自己去开启hibernate的session,很简单. 但是@Transactional里有很多属性一直没有用过...其中最让我在意的便是Propagation属性...其他属性都还是蛮好理解的..但是这个属性一直不怎么懂,也一直没怎么去研究.. 大部分情况下的确是使用@Transactional不用额外设置就OK了.因为默认的Propagation是Propa

Spring注解总结[email&#160;protected]和@Qualifier、@Resource

前言 由于能力有限,不会过于深入的探讨spring的注解,只会介绍一下注解的基本使用方法 @Autowired @Autowired可以帮我们注入一个属性,一般作用在普通方法之上(也可以作用的变量上或者构造器上) @Autowired是根据类型匹配的,所以如果有两个一样类型的参数的时候,会出错 看下下面的例子 public class Bean2 { @Override public String toString() { return "bean2...."; } } public