对于注解的理解

问题描述

@DubboProvider 是 自定义注解。其定义如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface DubboProvider {
    @AliasFor(annotation = Component.class, attribute = "value")
    String value() default "";
}

看了上边的一堆东西,你估计不太了解这个东西,上边这一堆东西,到底是啥?个人理解拆分为3块

1) @Target、@Retention、@Documented 注解

这三个是 java 中的元注解,看java中的定义如下:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}
 
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

以上四个,都是java中元注解(java8中 添加了Repeatable 注解,自动百度),你可能现在有点云里雾里o.o,注解是什么?元注解是什么?

注解:描述数据(代码本身也是一种数据)的数据(你也可以理解为代码的标签,表明代码是什么用途)

元注解:也是一种注解,只能标记(此处用标记,你可以理解注解是一种标签,那么它的作用就是标记)注解的注解。

对于上边 4 个元注解的具体用法,可以参考下边的文章:

https://www.cnblogs.com/huajiezh/p/5263849.html

https://blog.csdn.net/briblue/article/details/73824058

 

2)@Component 注解 

看到这,你应该能知道 注解 和 元注解了,那么 元注解 干嘛呢?我们来看看 @Component 的定义

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {

   /**
    * The value may indicate a suggestion for a logical component name,
    * to be turned into a Spring bean in case of an autodetected component.
    * @return the suggested component name, if any (or empty String otherwise)
    */
   String value() default "";

}

从上边可以得知,Component 是通过 元注解 标记(也理解为 通过元注解 组合)来生成的,那么可以把它叫做:一个由元注解组合而成的注解 – 组合注解

关于组合注解,可以参考下边的问题:

https://blog.csdn.net/qq_26525215/article/details/53523970

 

 

3)@AliasFor 注解 

它也是一种组合注解,那么它的具体用法可以参考

https://blog.csdn.net/wolfcode_cn/article/details/80654730


----------------------------------------------------------------------------------- 

说了这么多,根据个人理解,总结如下:

1、注解是一系列元数据(可以为代码的标签,也就是用来描述数据(代码本身也是一种数据)的数据)

2、使用自定义注解,可以明确类别,提高可读性

3、给以后做扩展带来一些方便(比如dubbo这一类操作,需要统一做一些操作)

 

还有一些参考文章:

java中的注解

http://www.jasongj.com/2016/01/17/Java1_%E6%B3%A8%E8%A7%A3Annotation

https://www.jianshu.com/p/e9329c8a59c2

 

元注解&组合注解:

https://www.hifreud.com/2017/06/27/spring-boot-08-composite-annotation

https://www.cnblogs.com/ludashi/p/6598806.html

http://wemedia.ifeng.com/13639490/wemedia.shtml

https://cloud.tencent.com/developer/article/1018008

https://cn.aliyun.com/jiaocheng/800260.html

https://www.cnblogs.com/MaxElephant/p/8086810.html

https://blog.csdn.net/qq_26525215/article/details/53523970

https://blog.csdn.net/xiaolyuh123/article/details/66968883

 

java8中新增注解@repeatable:

https://yanbin.blog/java8-repeatable-annotations

https://blog.csdn.net/aitangyong/article/details/54346342

转载于:https://www.cnblogs.com/qxynotebook/p/9704480.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值