网站首页 > java教程 正文
JAVA自定义注解
注解概念
注解是Java SE 5.0版本开始引入的概念,它是对java源代码的说明,是一种元数据(描述数据的数据)。
注解和注释的不同
- 注释 注释是对代码的说明,给代码的读者看,便于帮读者梳理业务逻辑;在程序代码中经常看到的以@ 开头的大部分是注解;
- 注解 注解也是对代码的说明,需要配合工具(解析它的代码)使用,参与代码的编译,给应用程序看的;
注解分类
注解以@开头,我们会在应用程序中见到各种各样的注解,比如 @Autowired,@Service,@Controller,@Override,@Test,@Value 等等,按照来源划分,可以分为 JDK的注解,第三方的注解,自定义注解。
JDK注解
JAVA 内置注解
- @Override (标记重写方法)
- @Deprecated (标记过时)
- @SuppressWarnings (忽略警告)
元注解 (注解的注解)
- @Target (注解的作用目标)
- @Retention (注解的生命周期)
- @Document (注解是否被包含在JavaDoc中)
- @Inherited (是否允许子类集成该注解)
第三方注解(各种框架注解)
请自行百度各个框架的注解详解
自定义注解
使用元注解自己定义的注解
JDK中有一些元注解,主要有@Target,@Retention,@Document,@Inherited用来修饰注解。
@Target
表明该注解可以应用的java元素类型
Target类型 描述 ElementType.TYPE 应用于类、接口(包括注解类型)、枚举 ElementType.FIELD 应用于属性(包括枚举中的常量) ElementType.METHOD 应用于方法 ElementType.PARAMETER 应用于方法的形参 ElementType.CONSTRUCTOR 应用于构造函数 ElementType.LOCAL_VARIABLE 应用于局部变量 ElementType.ANNOTATION_TYPE 应用于注解类型 ElementType.PACKAGE 应用于包 ElementType.TYPE_PARAMETER 1.8版本新增,应用于类型变量) ElementType.TYPE_USE 1.8版本新增,应用于任何使用类型的语句中(例如声明语句、泛型和强制转换语句中的类型)
@Retention
表明该注解的生命周期
生命周期类型 描述 RetentionPolicy.SOURCE 编译时被丢弃,不包含在类文件中 RetentionPolicy.CLASS JVM加载时被丢弃,包含在类文件中,默认值 RetentionPolicy.RUNTIME 由JVM 加载,包含在类文件中,在运行时可以被获取到
@Document
表明该注解标记的元素可以被Javadoc 或类似的工具文档化
@Inherited
表明使用了@Inherited注解的注解,所标记的类的子类也会拥有这个注解
注解格式
/**
* 修饰符 @interface 注解名 {
* 注解元素的声明1
* 注解元素的声明2
* }
* 修饰符:访问修饰符必须为public,不写默认为pubic;
* 关键字:必须为@interface;
* 注解名: 注解名称为自定义注解的名称,使用时还会用到;
* 注解类型元素:注解类型元素是注解中内容,可以理解成自定义接口的实现部分;
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
/**
* 注解的元素声明的两种形式
* type elementName();
* type elementName() default value;
*/
String value() default "test";
}
注解样例
接下来我们以Spring中的 @Service 注解为例
@Target({ElementType.TYPE})// ElementType.TYPE 代表在注解上使用
@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表运行时使用,可以通过反射获取到
@Documented//包含在JavaDoc中
@Component//允许通过包扫描的方式自动检测
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
@Annotation
JDK1.5有的,在rt.jar包下 java.lang.annotation包下,所有的注解默认继承了Annotation接口,但是它本身不能定义注解。
package java.lang.annotation;
/**
* 所有的注解默认继承了Annotation接口,但是它本身不能定义注解。
* The common interface extended by all annotation types. Note that an
* interface that manually extends this one does <i>not</i> define
* an annotation type. Also note that this interface does not itself
* define an annotation type.
*
* More information about annotation types can be found in section 9.6 of
* <cite>The Java? Language Specification</cite>.
*
* The {@link java.lang.reflect.AnnotatedElement} interface discusses
* compatibility concerns when evolving an annotation type from being
* non-repeatable to being repeatable.
*
* @author Josh Bloch
* @since 1.5
*/
public interface Annotation {
.
.
.
}
实现自定义注解
第一步-定义自定义注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
String value() default "test";
}
第二步-配置注解
@Data
@Builder
@MyTestAnnotation
public class MyBean {
private String name;
private int age;
}
第三步-利用反射解析注解
public class MyTest {
//isAnnotationPresent:判断当前元素是否被指定注解修饰
//getAnnotation:返回指定的注解
//getAnnotations:返回所有的注解
public static void main(String[] args) {
try {
//获取MyBean的Class对象
MyBean myBean = MyBean.builder().build();
Class clazz = myBean.getClass();
//判断myBean对象上是否有MyTestAnnotation注解
if (clazz.isAnnotationPresent(MyTestAnnotation.class)) {
System.out.println("MyBean类上配置了MyTestAnnotation注解!");
//获取该对象上MyTestAnnotation类型的注解
MyTestAnnotation myTestAnnotation = (MyTestAnnotation) clazz.getAnnotation(MyTestAnnotation.class);
System.out.println(myTestAnnotation.value());
} else {
System.out.println("MyBean类上没有配置MyTestAnnotation注解!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行main方法,运行结果:
Connected to the target VM, address: '127.0.0.1:62125', transport: 'socket'
MyBean类上配置了MyTestAnnotation注解!
test
Disconnected from the target VM, address: '127.0.0.1:62125', transport: 'socket'
参考链接:https://blog.csdn.net/zt15732625878/article/details/99671586
参考链接:https://blog.csdn.net/zt15732625878/article/details/100061528
赵小胖个人博客
猜你喜欢
- 2024-09-27 在Spring Boot项目中创建和使用自定义注解
- 2024-09-27 这一篇 Java 注解,写得太好了(java注解使用)
- 2024-09-27 学习廖雪峰的JAVA教程---注解(定义注解@interface)
- 2024-09-27 自定义注解妙用,一行代码搞定用户操作日志记录,你学会了吗?
- 2024-09-27 Spring Boot 整合mybatis,使用注解的方式(自动生成注解)
- 2024-09-27 面试官:实际工作中哪里用到了自定义注解?
- 2024-09-27 使用自定义注解和切面AOP实现Java程序增强
- 2024-09-27 Java-注解有什么用?该怎么用?(java 注解的作用)
- 2024-09-27 Java 17中的元注解:自定义注解的行为
- 2024-09-27 自定义注解你真会用吗?(说说自定义注解的场景及实现)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)