注解

一、常见的Java注解

Jdk自带的注解:@Override,@SuppressWarnings,@Deprecated(方法过时)

第三方框架注解:Spring,Mybatis等

二、注解的分类

1.按运行机制分

源码注解 源码存在,class文件不存在

编译时注解 源码,class文件存在

运行时注解 spring @antuAire

2.按来源分

Jdk自带的注解

第三方注解

自定义注解

3.元注解

给注解用的注解

三、注解语法

1.声明public @interface

2.成员以无参无异常方式声明

3.可以用default为成员指定一个默认值

int age() default 18;

4.成员的返回值类型是有限制的,合法的有基本数据类型,String,Class,Annotation,Enurmeration

5.如果注解只有一个成员,则成员名必须为value(),在使用时可以忽略成员名和赋值号(=)

6.注解类可以没有成员,没有成员的注解为标识注解

元注解

作用于注解上的注解,如@Target,@Retention,@Inherited,@Documented


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 自定义注解
* @author Yuwl
*/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {

String value();

}


Target注解:注解的作用域,用在哪个地方,包含Java的所有元素:

CONSTRUCTOR 构造方法

FiELD 字段

LOCAL_VERIABLE 局部变量

METHOD 方法

PACKAGE 包

TYPE 类接口

Retention注解:生命周期,包含:

SOURCE 源码

CLASS 编译

RUNNTIME 运行时

Inheriter注解:标识性元注解,允许子类继承,但只适用于类的继承,不能用于接口继承,而且只会继承类的注解,不会继承方法的

Documented注解:生成javadoc时会包含注解

注解的使用:

@注解名(成员名1=成员值1,成员名2=成员值2)

四、自定义注解

1.自定义注解
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 自定义注解
* @author Yuwl
*/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {

String value();

}


2.注解的使用
/**
* 自定义注解的使用
* @author Yuwl
*/
@Description("I am class annotation")
public class UseAnnotation {

@Description("I am method annotation")
public void hello(){

}

}


3.注解的解析
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* 解析注解
* @author Yuwl
*/
public class ParseAnnotation {

public static void main(String[] args) {
try {
//1.使用类加载器加载类
Class c = Class.forName("com.yuwl.ann.UseAnnotation");
//2.找到类上的注解
boolean exist = c.isAnnotationPresent(Description.class);
if(exist){
//3.拿到注解实例
Description d = (Description)c.getAnnotation(Description.class);
System.out.println(d.value());
}

//4.找到方法上的注解
Method[] ms = c.getMethods();
for(Method m : ms){
if(m.isAnnotationPresent(Description.class)){
Description d = (Description)m.getAnnotation(Description.class);
System.out.println(d.value());
}
}
//5.方法注解的另一种解析方式
for(Method m : ms){
Annotation[] ans = m.getAnnotations();
for(Annotation an : ans){
Description d = (Description)an;
System.out.println(d.value());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

}


注解的解析主要用到Java的反射。

原文地址:https://www.cnblogs.com/wlm-boke/p/8618693.html