认识注解
就是Java代码中的特殊标记,比如说:@Override、@Test等,作用是:让其他程序根据注解信息来决定怎么执行该程序
public @interface AnnotationTest2 {
String value();
int age();
}
public @interface MyTest1 {
String aaa();
boolean bbb() default true;
String ccc();
}
@AnnotationTest2(value = "111", age = 11)
public class AnnotationTeat1 {
@MyTest1(aaa = "111",bbb = false , ccc = "222")
public void test1(){
}
}
value是一个特殊变量,所以在使用时可以直接赋值,但是有其他变量存在时,就不可以这样赋值,可是当其他变量有默认值时,就可以直接赋值
元注解
指的是:修饰注解的注解
@Target声明被修饰的注解只能在哪些位置使用
@Target({ElementType.TYPE,ElementType.FIELD})
public @interface MyTest1 {
String aaa();
boolean bbb() default true;
String ccc();
}
@Retention声明注解的保留周期
@Retention(RetentionPolicy.RUNTIME)
注解的解析
判断类上、方法上、成员变量上是否存在注解,并把注解里的内容解析出来
import org.junit.Test;
import java.lang.reflect.Method;
public class AnnotationTest {
@Test
public void ParseClass() throws Exception {
//1.创建Class对象
Class<Demo> demoClass = Demo.class;
//2.解析类上面的注解
if(demoClass.isAnnotationPresent(MyTest.class)){
MyTest declaredAnnotation = demoClass.getDeclaredAnnotation(MyTest.class);
System.out.println(declaredAnnotation.value());
System.out.println(declaredAnnotation.aaa());
System.out.println(declaredAnnotation.bbb());
}
//获取方法
Method test1 = demoClass.getDeclaredMethod("test1");
//获取解析类的注解
if(test1.isAnnotationPresent(MyTest.class)){
MyTest declaredAnnotation = test1.getDeclaredAnnotation(MyTest.class);
System.out.println(declaredAnnotation.value());
System.out.println(declaredAnnotation.aaa());
System.out.println(declaredAnnotation.bbb());
}
}
}
@Target({ElementType.TYPE , ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest {
String value();
double aaa() default 100;
String[] bbb();
}
public class AnnotationTest {
@Test
public void ParseClass() throws Exception {
//1.创建Class对象
Class<Demo> demoClass = Demo.class;
//2.解析类上面的注解
if(demoClass.isAnnotationPresent(MyTest.class)){
MyTest declaredAnnotation = demoClass.getDeclaredAnnotation(MyTest.class);
System.out.println(declaredAnnotation.value());
System.out.println(declaredAnnotation.aaa());
System.out.println(declaredAnnotation.bbb());
}
//获取方法
Method test1 = demoClass.getDeclaredMethod("test1");
//获取解析类的注解
if(test1.isAnnotationPresent(MyTest.class)){
MyTest declaredAnnotation = test1.getDeclaredAnnotation(MyTest.class);
System.out.println(declaredAnnotation.value());
System.out.println(declaredAnnotation.aaa());
System.out.println(declaredAnnotation.bbb());
}
}
}
-
先写注解
-
用Demo类给注解赋值
-
最后调用方法,将注解解析
package com.itheima.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest1 {
}
package com.itheima.annotation;
import java.lang.reflect.Method;
public class AnnotationTest1 {
@MyTest1
public void test1(){
System.out.println(111);
}
public void test2(){
System.out.println(222);
}
public static void main(String[] args) throws Exception {
AnnotationTest1 annotationTest1 = new AnnotationTest1();
//1.创建Class对象
Class<AnnotationTest1> annotationTest1Class = AnnotationTest1.class;
//2.提取对象的所有方法
Method[] declaredMethod = annotationTest1Class.getDeclaredMethods();
//3.遍历看谁有注解
for (Method method : declaredMethod){
if(method.isAnnotationPresent(MyTest1.class)){
//出发当前方法执行
method.invoke(annotationTest1);
}
}
}
}
注解的一个作用,对方法做一个标记