@interface注解使用

这个注解是用来自定义注解的,比如,以下的语句就是定义了一个@ComponentScan的注解。

public @interface ComponentScan{
}
  • 一般我们在点开注解的源码都会看到一个这样的类。
  • 这个类中的每一个方法都会成为这个自定义注解的一个属性,方法的返回值类型会成为属性的类型。

  • 当我们使用@interface定义一个新的注解的时候,需要确定这个注解的生命周期、需要用到哪些地方。这时就需要用到注解的注解
  • @Retention用来确定这个注解的生命周期
  • @Target用于指定注解使用的目标范围
  • @Documented用于生成一些标注,没有实际作用

例如:@Configration注解的源码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    String value() default "";
}
  • 以上的代码使用了@interface定义了一个名为@Configuration的注解
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值