前言
记录学习历程,在学习笔记中有描述不正确的地方,欢迎小伙伴们评论指正。
正文
在进入SpringBoot启动类的@SpringBootApplication中,总是能看到@interface。那么@interface是什么呢?
首先注解@interface不是接口而是注解类。
在jdk1.5之后加入的功能,使用@interface自定义注解时,自动继承了 java.lang.annotation.Annotation 接口。在定义注解时,不能继承其他的注解或接口。
@interface用来声明一个注解,其中的每个方法实际上是声明了一个个配置参数;方法名就是参数名称;返回值就是参数类型,其中返回值类型只能是基本数据类型。可以通过default来声明参数的默认值。
@interface的语法规范咱们以@AutoConfigurationPackage为例参考格式如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
}