spring 自定义元注解

首先我们打开一个常见的元 注解 @Autowired
Ctrl + 左键 打开 进入:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

	/**
	 * Declares whether the annotated dependency is required.
	 * <p>Defaults to {@code true}.
	 */
	boolean required() default true;

}

主要部分 就是 上面@Target ,@Retention ,@Documented 元注解
需要注意的地方是 注解的类型只能定义 基础类型 还有 string ,class
只能用public和default来修饰,不能够抛出异常等 复杂操作
@Retention 注解
功能:指明修饰的注解的生存周期,即会保留到哪个阶段
@Target 注解
功能:指明了修饰的这个注解的使用范围,即被描述的注解可以用在哪里

推荐下说的详细的地址:
链接1
链接2

猜你喜欢

转载自blog.csdn.net/weixin_42118284/article/details/90260838