package denglianbin.java.annotation5;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})//表示该注解只能用在类型和方法上
@Retention(RetentionPolicy.RUNTIME) //表示这个注解会被反射
public @interface MyAnnotation {
String username();
String password();
}
package denglianbin.java.annotation5;
import java.lang.reflect.Method;
public class MyAnnotationtest {
@MyAnnotation(username = "admin", password = "1230987")
public void dosome(){
}
public static void main(String[] args) throws Exception{
Class c = Class.forName("denglianbin.java.annotation5.MyAnnotationtest");
Method method = c.getDeclaredMethod("dosome");
if(method.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
System.out.println(myAnnotation.username());
System.out.println(myAnnotation.password());
}
}
}