spring-tutorial/docs/01.Java/13.框架/01.Spring/01.Spring核心/28.Spring注解.md

5.4 KiB
Raw Permalink Blame History

title date order categories tags permalink
Spring 注解 2022-12-23 09:08:15 28
Java
框架
Spring
Spring核心
Java
框架
Spring
/pages/b6556f/

Spring 注解

Spring 注解驱动编程发展历程

  • 注解驱动启蒙时代Spring Framework 1.x
  • 注解驱动过渡时代Spring Framework 2.x
  • 注解驱动黄金时代Spring Framework 3.x
  • 注解驱动完善时代Spring Framework 4.x
  • 注解驱动当下时代Spring Framework 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

Spring 注解编程模型

  • 元注解Meta-Annotations
  • Spring 模式注解Stereotype Annotations
  • Spring 组合注解Composed Annotations
  • Spring 注解属性别名和覆盖Attribute Aliases and Overrides

Spring 元注解Meta-Annotations

  • java.lang.annotation.Documented
  • java.lang.annotation.Inherited
  • java.lang.annotation.Repeatable

Spring 模式注解Stereotype Annotations

理解 @Component “派⽣性”:元标注 @Component 的注解在 XML 元素 context:component-scan 或注解 @ComponentScan 扫描中“派生”了 @Component 的特性,并且从 Spring Framework 4.0 开始支持多层次“派⽣性”。

举例说明:

  • @Repository
  • @Service
  • @Controller
  • @Configuration
  • @SpringBootConfigurationSpring Boot

@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.StandardAnnotationMetadata
  • 注解元信息 - org.springframework.core.type.AnnotationMetadata
  • ASM 实现 - org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor
  • 反射实现 - org.springframework.core.type.StandardAnnotationMetadata

Spring 组合注解Composed Annotations

Spring 组合注解Composed Annotations中的元注允许是 Spring 模式注解Stereotype Annotation与其他 Spring 功能性注解的任意组合。

Spring 注解属性别名Attribute Aliases

Spring 注解属性覆盖Attribute Overrides

Spring @Enable 模块驱动

@Enable 模块驱动

@Enable 模块驱动是以 @Enable 为前缀的注解驱动编程模型。所谓“模块”是指具备相同领域的功能组件集合,组合所形成⼀个独⽴的单元。⽐如 Web MVC 模块、AspectJ 代理模块、Caching缓存模块、JMXJava 管理扩展模块、Async异步处理模块等。

举例说明

  • @EnableWebMvc
  • @EnableTransactionManagement
  • @EnableCaching
  • @EnableMBeanExport
  • @EnableAsync

@Enable 模块驱动编程模式

  • 驱动注解:@EnableXXX
  • 导入注解:@Import 具体实现
  • 具体实现
  • 基于 Configuration Class
  • 基于 ImportSelector 接口实现
  • 基于 ImportBeanDefinitionRegistrar 接口实现

Spring 条件注解

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

  • 关联对象 - org.springframework.core.env.Environment 中的 Profiles
  • 实现变化:从 Spring 4.0 开始,@Profile 基于 @Conditional 实现

基于编程条件注解 - @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
    • org.springframework.context.annotation.ConfigurationClassParser

参考资料