CRUD工程师——Spring注解驱动

先放上一张网上找来的图

 

 模式注解使框架的配置变得简洁明了,从Spring Framework 3.1开始Spring开始全面支持面向注解配置,其中一些核心注解如下

装配注解:

 

 

 

 依赖注入注解如下表所示:

Bean定义注解如下表所示

 

Spring条件装配注解:

配置属性注解:

生命周期回调注解

 

 

 

2. Spring注解编程模型
主要有四个方面分别是:
元注解(Meta-Annotations)
Spring模式注解(Stereotype Annotations)
Spring组合注解(Composed Annotations)
Spring注解属性别名和覆盖(Attribute Aliases and Overrides)
2.1 元注解(Meta-Annotations)
元注解指一个能声明在其他注解上的注解,如果一个注解标注在其他注解上,那么他就是元注解。根据Java语言规范,注解之间不存在继承关系,因此可以通过元注解来注解到另一个注解达到“派生”的作用,这种“派生”特性需要确保注解之间的属性方法签名完全一致.

2.2 Spring模式注解(Stereotype Annotations)

简而言之Stereotype Annotations就是说明组件扮演的角色,以@Component注解为例,@Service、@Repository、@Controler、@RestController及@Configuration都包含@Component注解,所以包含@Component的功能,同时他们在不同的场景下扮演不同的角色。在Spring Framework 4.x 之前以@Component注解为例,只能识别两层的@Component派生注解,在Spring Framework 4.x 之后可以递归识别多层次的@Component派生注解。
需要另外注意的是Spring在扫描的过程中还会有excludeFilters和includeFilters,排除excludeFilters中的注解,属于includeFilters中的注解将会通过筛选条件(默认初始化时includeFilters会加入@Component)。
还有一点是,处于性能方面的Spring获取底层元数据的方式是通过ASM实现的,而不是通过反射,如ClassReader类,相对于ClassLoader体系,Spring ASM更为底层,读取的是类资源,直接操作的是其中的字节码,获取相关元信息,在读取元信息方面Spring抽象出MetadataReader接口。

2.3 Spring组合注解(Composed Annotations)

其目的在于将多个注解行为组合成单个自定义注解,比如@SpringBootApplication注解由@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan等几个注解组成,Spring并没有考虑通过Java反射的手段来解析元注解信息,而是抽象出AnnotationMetadata接口,其实现类为AnnotationMetadataReadingVisitor,从Spring 4.0开始,在初始化过程中AnnotationMetadataReadingVisitor所关联的AnnotationAttributesReadingVisitor采用递归查找元注解,并保存在AnnotationMetadataReadingVisitor的metaAnnotationMap字段中。
2.4 Spring注解属性别名和覆盖(Attribute Aliases and Overrides)
在 Spring 中,有一些注解,使用不同属性方法,却能到达相同结果。典型的如 RequestMapping
使用 @AliasFor 注解,可以做到别名的功能。
在 Spring 中别名可以分为以下几类:
显式别名(xplicit Aliases)
隐式别名(Implicit Aliases)
传递隐式别名(Transitive Implicit Aliases)
以上三类都需要满足以下条件:
属性类型相同
属性方法必须存在默认值
属性默认值必须相同
否则运行过程中将会出错。

 

 

 

 
 
 
 
posted @ 2020-06-23 19:45  smartcat994  阅读(247)  评论(0编辑  收藏  举报