Spring 注解驱动编程发展历程

1.2 @Transactional
@ManagedResource
2.0 @Component
@Repository
3.0 @Bean
@Import

4.0 @Profile
@Conditional
@Indexed
Spring 核心注解场景分类

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

装配注解

|注解|场景说明|起始版本|
|---|---|---|
|@ImportResource| 替换XML元素\<import\>| 2.5|
|@Import| 导入Configuration类,无论是否标记@Configuration| 2.5|
|@ComponentScan| 扫描指定package下标注Spring模式注解的类| 3.1|

依赖注入注解

|注解|场景说明|起始版本|
|---|---|---|
|@Autowired| Bean依赖注入,支持多种依赖查找方式| 2.5|
|@Qualifier| 细粒度的@Autowired查找| 2.5|

Spring 注解编程模型

元注解
Spring 模式注解
Spring 组合注解
Spring 注解属性别名/覆盖

Spring 元注解(Meta-Annotations)

A meta-annotation is an annotation that is declared on another annotation.
An annotation is therefore meta-annotated if it is annotated with another annotation. For example, any annotation that is declared to be documented is meta-annotated with @Documented from the java.lang.annotation package.

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

Spring 模式注解(Stereotype Annotations)

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

  • @Repository
  • @Service
  • @Controller
  • @Configuration
  • @SpringBootConfiguration

Spring 组合注解(Composed Annotations)

A composed annotation is an annotation that is meta-annotated withy one or more annotations with the intent of combining the behavor associated with those meta-annotations into a single custom annotation.
For example, an annotation named @TransactionalService that is meta-annotation with Spring’s @Transactional and @Service annotations is a composed annotation that combines the semantics of @Transactional and @Service.
@TransactionalService is technically also a custom stereotype annotation.

基本定义:

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

Spring 注解属性别名(Attribute Alias)

Spring 注解属性覆盖(Attribute Ovverides)

Spring @Enable 模块驱动

@Import(@Configuration)
@Import(ImportSelector)
@Import(ImportBeanDefinitionRegistrar)
AnnotatedBeanDefinition ad = new AnnotatedGenericBeanDefinition();
BeanDefinitionReaderUtils.registerWithGeneratedName(ad, register);

Spring 条件注解

上下文对象: org.springframework.context.annotation.ConditionContext
条件判断:org.springframework.context.annotation.ConditionEvaluator
配置阶段:org.sprignframework.context.annotation.ConfigurationCondition.ConfigurationPhase
判断入口:org.springframework.context.annotation.ConfigurationClassPostProcessor
org.springframework.context.annotation.ConfigurationClassPasser
ConditionContext
ConditionEvaluator