{"action":"opened","issue":{"url":"https://api.github.com/repos/doctorwho1986/MyBlog/issues/6","labels_url":"https://api.github.com/repos/doctorwho1986/MyBlog/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/doctorwho1986/MyBlog/issues/6/comments","events_url":"https://api.github.com/repos/doctorwho1986/MyBlog/issues/6/events","html_url":"https://github.com/doctorwho1986/MyBlog/issues/6","id":57458943,"number":6,"title":"java方法注解获取注解值和方法参数注解获取注解值(mybatis注解，spring注解）","user":{"login":"doctorwho1986","id":6966316,"avatar_url":"https://avatars.githubusercontent.com/u/6966316?v=3","gravatar_id":"","url":"https://api.github.com/users/doctorwho1986","html_url":"https://github.com/doctorwho1986","followers_url":"https://api.github.com/users/doctorwho1986/followers","following_url":"https://api.github.com/users/doctorwho1986/following{/other_user}","gists_url":"https://api.github.com/users/doctorwho1986/gists{/gist_id}","starred_url":"https://api.github.com/users/doctorwho1986/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/doctorwho1986/subscriptions","organizations_url":"https://api.github.com/users/doctorwho1986/orgs","repos_url":"https://api.github.com/users/doctorwho1986/repos","events_url":"https://api.github.com/users/doctorwho1986/events{/privacy}","received_events_url":"https://api.github.com/users/doctorwho1986/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-02-12T13:19:59Z","updated_at":"2015-02-12T13:19:59Z","closed_at":null,"body":"'''\r\npackage com.github.jdk.annotation;\r\n\r\nimport java.lang.annotation.Annotation;\r\nimport java.lang.annotation.Documented;\r\nimport java.lang.annotation.ElementType;\r\nimport java.lang.annotation.Retention;\r\nimport java.lang.annotation.RetentionPolicy;\r\nimport java.lang.annotation.Target;\r\nimport java.lang.reflect.Method;\r\n\r\n/**\r\n * 方法注解获取注解值和方法参数注解获取注解值\r\n * @author doctor\r\n *\r\n */\r\npublic class JavaAnnotationsExample {\r\n\r\n\tpublic static void main(String[] args) throws ReflectiveOperationException {\r\n\t\tMethod[] methods = JavaAnnotationClass.class.getMethods();\r\n\t\tfor (Method method : methods) {\r\n\t\t\tif (method.isAnnotationPresent(JavaAnnotationsEx.class)) {\r\n\t\t\t\tSystem.out.println(\"method:\"+ method);\r\n\t\t\t\t\r\n\t\t\t\t//获得一个注解实例。这是\r\n\t\t\t\tJavaAnnotationsEx annotation = method.getAnnotation(JavaAnnotationsEx.class);\r\n\t\t\t\tSystem.out.println(\"JavaAnnotationsEx:\" + annotation);\r\n\t\t\t\t\r\n\t\t\t\t//获得此注解实例的注解类型（Returns the annotation type of this annotation），和一般类获取class不同的地发\r\n\t\t\t\t//反射获得它的方法\r\n\t\t\t\tMethod[] declaredMethods = annotation.annotationType().getDeclaredMethods();\r\n\t\t\t\tfor (Method method2 : declaredMethods) {\r\n\t\t\t\t\tSystem.out.println(method2);\r\n\t\t\t\t\tSystem.out.println(\"method name:\" + method2.getName() + \"--value:\" + method2.invoke(annotation));\r\n\t\t\t\t}\r\n\t\t\t\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t \r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\tSystem.out.println(\"方法参数注解获取值\");\r\n\t\t\r\n\t\tfor (Method method : methods) {\r\n\t\t\t//方法获取参数注解示例。二维数组\r\n\t\t\tAnnotation[][] parameterAnnotations = method.getParameterAnnotations();\r\n\t\t\tfor (Annotation[] annotations : parameterAnnotations) {\r\n\t\t\t\tfor (Annotation annotation : annotations) {\r\n\t\t\t\t\tif (annotation instanceof JavaAnnotationsEx) {\r\n\t\t\t\t\t\tJavaAnnotationsEx a = (JavaAnnotationsEx) annotation;\r\n\t\t\t\t\t\tSystem.out.println(\"注解是：\" + a);\r\n\t\t\t\t\t\tSystem.out.println(\"直接调用注解的方法，获取注解值，\");\r\n\t\t\t\t\t\tSystem.out.println(\"age:\" + a.age());\r\n\t\t\t\t\t\tSystem.out.println(\"name:\" + a.name());\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\tSystem.out.println(\"反射获取参数注解的值：\");\r\n\t\t\t\t\t\tMethod[] methodsA = a.annotationType().getDeclaredMethods();\r\n\t\t\t\t\t\tfor (Method m : methodsA) {\r\n\t\t\t\t\t\t\tSystem.out.println(\"method name:\" + m.getName() + \"--value:\"+ m.invoke(a));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t}\r\n\t\r\n\t@Target({ElementType.METHOD,ElementType.PARAMETER})\r\n\t@Retention(RetentionPolicy.RUNTIME)\r\n\t@Documented\r\n\tpublic @interface JavaAnnotationsEx{\r\n\t\tString name()default \"\";\r\n\t\tint age() default 0;\r\n\t}\r\n\t\r\n\tpublic static class JavaAnnotationClass{\r\n\t\t\r\n\t\t@JavaAnnotationsEx(name=\"doctor\",age=666666)\r\n\t\tpublic String method(@JavaAnnotationsEx(name=\"who\") String name){\r\n\t\t\treturn name;\r\n\t\t}\r\n\t}\r\n\r\n}\r\n\r\n'''"}}