@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的注解

你可能感兴趣的:(Spring,java,spring)