Spring-boot学习-注解基础

在注解的开发过程中,如果不懂注解的基础知识,那么在开发过程中就不能理解Spring的注解,所以开发也会迷迷糊糊,出了问题会不知所措,如果明白了基础知识,在自己开发的代码中出了问题,则对比官方的方法就能很快找出问题,也能根据需求开发出能够最好满足于自己业务需求的注解插件。同时对于Spring的整体把控也会更好
当然,不了解,一般情况下也不会出问题,但是为了自己的成长更加扎实,所以,就整理了自己最近看源码的一些收获

本文只讨论基本概念,其实现原理,后续再讨论 

目录

Meta-Annotations(元注解)

Stereotype Annotations(模式注解) 

Composed Annotations(组合注解)

Attribute Aliases and Overrides(属性别名和覆盖)



Meta-Annotations(元注解)

Wiki给出的定义

  • 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.annoation packshge.
  • 所以,元注解是可以标注在其他注解上面的注解,且并没有限定Spring,所以应该是在Java环境而言都可以这么定义
    • Java5 定义的元注解
      • @Target--Annotation所修饰的对象范围
        • 1.CONSTRUCTOR:用于描述构造器
        • 2.FIELD:用于描述域
        • 3.LOCAL_VARIABLE:用于描述局部变量
        • 4.METHOD:用于描述方法
        • 5.PACKAGE:用于描述包
        • 6.PARAMETER:用于描述参数
          7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
      • @Retention-Annotation被保留的时间长短
        • 1.SOURCE:在源文件中有效(即源文件保留)
        • 2.CLASS:在class文件中有效(即class保留)
        • 3.RUNTIME:在运行时有效(即运行时保留)
      • @Documented-标记注解
        • 表明这个注解应该被 javadoc工具记录
          • 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员
      • @Inherited-标注解
        • 允许子类继承父yi的注解。
          • 注解本身标记此注解,那么父类的此注解其可以拥有,可以理解为标记注解父子类之间的传递性
        • 注解本身是不可以进行继承开发的

Java8中的@Repeatable

Spring中的@Component 

  • Wiki中也做了介绍,只要能够在类上面做标注的都可以称之为元注解
    • @Component is a generic stereotype for any Spring-managed component. Any component annotated with @Component is a candidate for component scanning. Similarly, any component annotated with an annotation that is itself meta-annotated with @Component is also a candidate for component scanning. For example, @Service is meta-annotated with @Component.

      Core Spring provides several stereotype annotations out of the box, including but not limited to: @Component, @Service, @Repository, @Controller, @RestController, and @Configuration. @Repository, @Service, etc. are specializations of @Component.

Stereotype Annotations(模式注解) 

从Wiki的定义去理解这个概念(Wiki中给出的定义

A stereotype annotation is an annotation that is used to declare the role that a component plays within the application. For example, the @Repository annotation in the Spring Framework is a marker for any class that fulfills the role or stereotype of a repository (also known as Data Access Object or DAO).
从第一句中,我们可以看出,模式注解的作用是用来说明一个组件在应用中的角色,后面又给出了@Repository的例子

第二段:@Component is a generic stereotype for any Spring-managed component. Any component annotated with @Component is a candidate for component scanning. Similarly, any component annotated with an annotation that is itself meta-annotated with @Component is also a candidate for component scanning. For example, @Service is meta-annotated with @Component.
这段话开头就直接告我们,Spring管理的所有组件都是基于@Component的!

我个人对Stereotype Annotations的理解为:Spring根据@Component,把Spring架构需要的内容划分出各种角色,派生出各种具体的组件,例如:

  • @Service--服务层组件
  • @Contoller--控制器组件
  • @Configuration--配置组件

业界大牛小马哥对Stereotype Annotations的定义:@Component“派生”注解
之所以不是继承而是派生,是因为Java规范不允许Annotation继承

Composed Annotations(组合注解)

 Wiki定义

A composed annotation is an annotation that is meta-annotated with one or more annotations with the intent of combining the behavior associated with those meta-annotations into a single custom annotation. For example, an annotation named @TransactionalService that is meta-annotated 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.
第一句给出了Composed Annotations的定义:将一个或者多个Annotation组合在一起形成的注解

Attribute Aliases and Overrides(属性别名和覆盖)

Wiki

An attribute alias is an alias from one annotation attribute to another annotation attribute.
属性别名:一个annotation的属性和另一个属性的相同 

An attribute override is an annotation attribute that overrides (or shadows) an annotation attribute in a meta-annotation.
属性覆盖:一个annotation的属性覆盖了元注解中的属性

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值