如何阅读JDK和Spring中关于注解代码的浅析

4 篇文章 0 订阅

 

1.JDK中的@Resource注解

@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
  
    String name() default "";

    String lookup() default "";

    Class<?> type() default java.lang.Object.class;

    enum AuthenticationType {
            CONTAINER,
            APPLICATION
    }

    AuthenticationType authenticationType() default AuthenticationType.CONTAINER;

    boolean shareable() default true;

    String mappedName() default "";

    String description() default "";
}

@Target({TYPE, FIELD, METHOD}) : 引入该注解,表示该注解可用的范围,上述@Resource注解可用于类,字段,方法上。

@Retention(RUNTIME) : 引入该注解,表示该注解可保留的范围,RUNTIME表示在jvm运行时也保留。

String name() default "":表示注解中可选的name字段,default设置默认值。

后面定义了一个枚举类型来表示@Resoure的两种身份认证类型。

2.Spring中的@RequestMapping注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

@Target,@Retention同上不再详细说了。

@Documented,引入这个注解表明这个注释是由 javadoc记录的,在默认情况下也有类似的记录工具。 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分。

@Mapping,引入该注解。

RequestMethod[] method() default {}中定义了方法得请求方式GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值