参数文献

Spring注解驱动编程发展历程

  • 注解驱动启蒙时代: Spring Framerwork 1.x
  • 注解驱动过渡时代: Spring Framerwork 2.x
  • 注解驱动黄金时代: Spring Framerwork 3.x
  • 注解驱动完善时代: Spring Framerwork 4.x
  • 注解驱动当下时代: Spring Framerwork 5.x

Spring核心注解场景分类

  • Spring模式注解

    Spring注解 场景说明 起始版本
    @Repository 数据仓储模式注解 2.0
    @Component 通用组件模式注解 2.5
    @Service 服务模式注解 2.5
    @Controller Web控制器模式注解 2.5
    @Configuration 配置类模式注解 3.0
  • 装配注解

    Spring注解 场景说明 起始版本
    @ImportResource 替换XML元素<import> 2.5
    @Import 导入Configuration 2.5
    @ComponentScan 扫描指定package下标注Spring模式注解的类 3.1
  • 依赖注入注解

    Spring注解 场景说明 起始版本
    @Autowired Bean依赖注入,支持多种依赖查找方式 2.5
    @Qualifier 细粒度的@Autowired依赖查找 2.5
    Java注解
    @Resource Bean依赖注入,仅支持名称依赖查找 2.5
    • 无论@Autowired注入单个Spring Bean,还是注入Spring Bean集合,其依赖查找的实现均属于限定类型(Class)方式,若需要在相同类型中再细粒度的筛选,则需要只使用@Qualifier配合.
  • Bean定义注解

    Spring注解 场景说明 起始版本
    @Bean 替换XML元素<bean/> 3.0
    @DeoendsOn 替换XML元素<bean depends-on="..."/> 3.0
    @Lazy 替换XML元素`<bean lazy-init="true false"/>`
    @Primary 替换XML元素`<bean primary="true false"/>`
    @Role 替换XML元素<bean role="..."/> 3.1
    @Lookup 替换XML元素<bean lookup-method="..."/> 4.1
  • Spring条件装配注解

    Spring注解 场景说明 起始版本
    @Profile 配置化条件装配 3.1
    @Conditional 编程条件装配 3.1
  • 配置属性注解

    Spring注解 场景说明 起始版本
    @PropertySource 配置属性抽象PropertySource注解 3.1
    @PropertySources @PropertySource集合注解 4.0
  • 生命周期回调注解

    Spring注解 场景说明 起始版本
    @ProConstruct 替换XML元素<bean init-method="..."/> 2.5
    @PreDestory 替换XML元素<bean destroy-method="..."/> 2.5
  • 注解属性注解

    Spring注解 场景说明 起始版本
    @AliasFor 别名注解属性,实现复用的目的 4.2
  • 性能注解

    Spring注解 场景说明 起始版本
    @Indexed 提升Spring模式注解的扫描效率 5.0

Spring注解编程模型

  • 编程模型

    • 元注解(Meta-Annotations)

      • java.lang.annotation.Documented
      • java.lang.annotation.Inherited
      • java.lang.annotation.Repeatable
    • Spring模式注解(Stereotype Annotations)

      • @Repository
      • @Component
      • @Service
      • @Configuration
      • org.springframework.boot.SpringBootConfiguration
      • @Component"派生性"原理
        • 核心组件:
          • org.springframework.context.annotation.ClassPathBeanDefinitionScanner
          • org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
        • 资源处理
          • org.springframework.core.io.support.ResourcePatternResolver
        • 资源-类元信息
          • org.springframework.core.type.classreading.MetadataReaderFactory
        • 类元信息
          • org.springframework.core.type.ClassMetadata
          • ASM实现: org.springframework.core.type.classreading.ClassMetadataReadingVisitor org.springframework.core.type.classreading.SimpleAnnotationMetadataReadingVisitor
          • 反射实现: org.springframework.core.type.StandardAnnotationMetadata
        • 注解元信息
          • org.springframework.core.type.AnnotationMetadata
          • ASM实现: org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor org.springframework.core.type.classreading.SimpleAnnotationMetadataReadingVisitor
          • 反射实现: org.springframework.core.type.StandardAnnotationMetadata
    • Spring组合注解(Composed Annotations)

    • Spring注解属性别名和覆盖(Attribute Aliases and Overrides)

      Attribute Aliases and Overrides

      An attribute alias is an alias from one annotation attribute to another annotation attribute. Attributes within a set of aliases can be used interchangeably and are treated as equivalent. Attribute aliases can be categorized as follows.

      1. Explicit Aliases: if two attributes in one annotation are declared as aliases for each other via @AliasFor, they are explicit aliases.
      2. Implicit Aliases: if two or more attributes in one annotation are declared as explicit overrides for the same attribute in a meta-annotation via @AliasFor, they are implicit aliases.
      3. Transitive Implicit Aliases: given two or more attributes in one annotation that are declared as explicit overrides for attributes in meta-annotations via @AliasFor, if the attributes effectively override the same attribute in a meta-annotation following the law of transitivity, they are transitive implicit aliases.

      An attribute override is an annotation attribute that overrides (or shadows) an annotation attribute in a meta-annotation. Attribute overrides can be categorized as follows.

      1. Implicit Overrides: given attribute A in annotation @One and attribute A in annotation @Two, if @One is meta-annotated with @Two, then attribute A in annotation @One is an implicit override for attribute A in annotation @Two based solely on a naming convention (i.e., both attributes are named A).
      2. Explicit Overrides: if attribute A is declared as an alias for attribute B in a meta-annotation via @AliasFor, then A is an explicit override for B.
      3. Transitive Explicit Overrides: if attribute A in annotation @One is an explicit override for attribute B in annotation @Two and B is an explicit override for attribute C in annotation @Three, then A is a transitive explicit override for C following the law of transitivity.

Spring @Enable模块驱动

  • @Enable模块驱动

    @Enable模块驱动是以@Enable为前缀的注解驱动编程模型.所谓"模块"是指具备相同领域的功能组件集合,组合所形成一个独立的单元.比如Web MVC模块,AspectJ代理模块,Caching(缓存)模块,JMX(Java管理扩展)模块,Async(异步处理)模块等

  • 示例

    • @EnableWebMvc
    • @EnabTransactionManagement
    • @EnableCaching
    • @EnableMBeanExport
    • @EnableAsync
  • @Enable模块驱动编程模式

    • 驱动注解: @EnableXXX

    • 导入注解: @Import具体实现

    • 具体实现:

      • 基于注解编程: 基于@Configuration Class, 可参考@EnableWebMvc

        1
        2
        3
        4
        5
        6
        7
        @Retention(RetentionPolicy.RUNTIME)
        @Target(ElementType.TYPE)
        @Documented
        @Import(DelegatingWebMvcConfiguration.class)
        public @interface EnableWebMvc {
        }

      • 基于接口编程: 基于ImportSelector接口实现或者基于ImportBeanDefinitionRegistrar接口实现

        1
        2
        3
        4
        5
        6
        7
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @Import(CachingConfigurationSelector.class)
        public @interface EnableCaching {
        //...略
        }

Web自动装配

1
2
3
4
WebApplicationInitializer
|- AbstractContextLoaderInitializer
|- AbstractDispatcherServletInitializer
|- AbstractAnnotationConfigDispatcherServletInitializer
  • AbstractContextLoaderInitializer: 如果构建Web Root应用上下文(WebApplicationContext)成功则替代web.xml注册ContextLoaderListener

  • AbstractDispatcherServletInitializer: 替代web.xml注册DispatcherServlet,并且如果必要的话,创建Web Root应用上下文(WebApplicationContext)

  • AbstractAnnotationConfigDispatcherServletInitializer: 具备Annotation配置驱动能力的AbstractDispatchServletInitializer

    Spring条件注解

    • 基于配置条件注解: @org.springframework.context.annotation.Profile

      • 关联对象: org.springframework.core.env.Environment中的Profiles

      • 实现变化: 从Spring 4.0开始,@Profile基于@Conditional

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        @Target({ElementType.TYPE, ElementType.METHOD})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @Conditional(ProfileCondition.class)
        public @interface Profile {

        /**
        * The set of profiles for which the annotated component should be registered.
        */
        String[] value();

        }
    • 基于编程条件注解: @org.springframework.context.annotation.Conditional

      • 关联对象: org.springframework.context.annotation.Condition具体实现
    • @Conditional实现原理

      • 上下文对象: org.springframework.context.annotation.ConditionContext
      • 条件判断: org.springframework.context.annotation.ConditionEvaluator
      • 配置阶段: org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase
      • 判断入口: org.springframework.context.annotation.ConfigurationClassPostProcessor
    • Spring Framework注解

      注解 激活模块
      @EnableWebMvc Web MVC模块
      @EnableTransactionManagement 事务管理模块
      @EnableCaching Caching模块
      @EnableMBeanExport JMX模块
      @EnableAsync 异步处理模块
      @EnableWebFlux Web Flux模块
      @EnableAspectJAutoProxy AspectJ 代理模块
    • Spring Boot注解

      注解 场景说明 起始版本
      @SpringBootConfiguration Spring Boot配置类 1.4.0
      @SpringBootApplication Spring Boot应用引导注解 1.2.0
      @EnableAutoConfiguration Spring Boot激活自动装配 1.0.0
      @EnableMangementContext Actuator管理模块
      @EnableConfigurationProperties 配置属性绑定模块
      @EnableOAuth2Sso OAuth2单点登录模块
    • Spring Cloud注解

      注解 场景说明 起始版本
      @SpringCloudApplication Spring Cloud应用引导注解 1.0.0
      @EnableDiscoveryClient Spring Cloud激活服务发现客户端注解 1.0.0
      @EnableCircuitBreaker Spring Cloud激活熔断注解 1.0.0
      @EnableEurekaServer Eureka服务器模块
      @EnableConfigServer 配置服务器模块
      @EnableFeignClients Feign客户端模块
      @EnableZuulProxy 服务网关Zuul模块