java通过java.lang.reflect.Method方法获取方法上面的注解。



import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface GetView {

	String Method();

	String Value();

}

public class Temp {

	@GetView(Method = "aa", Value = "bb")
	public void test() {
		System.out.println("In test method.");
	}

	public static void main(String[] args) throws ClassNotFoundException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, SecurityException, NoSuchMethodException {

		Temp temp = new Temp();
		Method method = temp.getClass().getMethod("test");
		System.out.println(method.isAnnotationPresent(GetView.class));
		method.invoke(temp);
		GetView view = method.getAnnotation(GetView.class);
		System.out.println(view.Method());
		System.out.println(view.Value());
	}

}