一、什么是注解

二、自定义注解

public @interface MyAnnotation {
boolean bbb() default true;
@MyAnnotation ( aaa ="牛魔王",ccc = "sss")
@MyAnnotation ( aaa = "aaa",ccc = "666")
三、注解的原理

四、元注解


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention ( RetentionPolicy.RUNTIME )
@Target ( {ElementType.TYPE,ElementType.METHOD} )
public @interface MyTest3 {
五、注解的解析


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention ( RetentionPolicy.RUNTIME )
@Target ( {ElementType.TYPE,ElementType.METHOD} )
public @interface MyTest4 {
double aaa() default 100;
public void parseClass(){
if (c.isAnnotationPresent ( MyTest4.class )){
MyTest4 myTest4= (MyTest4) c.getDeclaredAnnotation ( MyTest4.class );
System.out.println ( myTest4.value () );
System.out.println ( myTest4.bbb () );
六、应用场景:模拟Junit框架

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention ( RetentionPolicy.RUNTIME )
@Target ( ElementType.METHOD )
public @interface MyTest {
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
System.out.println ("===========tes1========");
System.out.println ("===========tes2========");
System.out.println ("===========tes3========");
System.out.println ("===========tes4========");
public static void main(String[] args) throws Exception {
Test2 test = new Test2 ();
Method[] methods = c.getDeclaredMethods ();
for (Method method : methods) {
if (method.isAnnotationPresent ( MyTest.class )) {