1. The first is custom annotations:
Java code
- PACKAGE CN.VEJI.HIBERNATE.PO;  
-
- import java.lang.annotation.elementtype;
- import java.lang.annotation.retention;
- span class= "keyword" >import java.lang.annotation.retentionpolicy;
- IMPORT JAVA.LANG.ANNOTATION.TARGET;  
-
- @Target (Elementtype.method)
- @Retention ( Retentionpolicy.runtime)
- public @ interface privilege {
- string[] value ( );
-
- }
2. define the use of the annotation class:
Java code
- Package Cn.veji.hibernate.po;
- Package Cn.veji.hibernate.po;
- Public class Testprivilege {
- @Privilege ({ "a"})
- public Void A () {
- }
- @Privilege ({ "B"})
- public void B () {
- }
- @Privilege ({ "C"})
- public void C () {
- }
- }
3. read the annotation value Help class:
Java code
- Package Cn.veji.hibernate.po;
- Package cn.veji.hibernate.test;
- Import java.lang.annotation.Annotation;
- Import Java.lang.reflect.Method;
- Import Java.util.HashMap;
- Import Java.util.Map;
- Import Cn.veji.hibernate.po.Privilege;
- Import Cn.veji.hibernate.po.TestPrivilege;
- Public class Annotationutil {
- public static Annotationutil anno = null;
- public static Annotationutil getinstance () {
- if (anno = = null) {
- Anno = new Annotationutil ();
- }
- return anno;
- }
- /**
- * Read annotation values
- *
- * @param annotationclasss Processing Annotation class name
- * @param annotationfield Processing Annotation Class Property names
- * @param className processing annotation Use class name
- * @return
- * @throws Exception
- */
- @SuppressWarnings ("All")
- Public map<string, string> Loadvlaue (Class annotationclasss,
- String Annotationfield, String className) throws Exception {
- SYSTEM.OUT.PRINTLN ("processing annotation class name = = =" +annotationclasss.getname ());
- SYSTEM.OUT.PRINTLN ("Processing annotation class Property name = = =" +annotationfield);
- SYSTEM.OUT.PRINTLN ("process annotation call Class name = = =" +classname);
- map<string, string> map = new hashmap<string, string> ();
- Method[] methods = Class.forName (ClassName). Getdeclaredmethods ();
- For (Method method:methods) {
- if (method.isannotationpresent (annotationclasss)) {
- Annotation p = method.getannotation (ANNOTATIONCLASSS);
- Method m = P.getclass ()
- . Getdeclaredmethod (Annotationfield, null);
- String[] values = (string[]) M.invoke (p, null);
- For (String key:values) {
- System.out.println ("annotated value = = =" + key);
- Map.put (key, key);
- }
- }
- }
- SYSTEM.OUT.PRINTLN ("map quantity = = =" + map.size ());
- return map;
- }
- public static void Main (string[] args) throws Exception {
- Annotationutil.getinstance (). Loadvlaue (Privilege. Class, "value",
- Testprivilege. class.getname ());
- }
- }
4. Implementation results:
Java code
- Process annotation class name = = = Cn.veji.hibernate.po.Privilege
- Handling Annotation Class Property names = = = value
- Call class name for processing annotation = = = Cn.veji.hibernate.po.TestPrivilege
- Note value = = = C
- Note value = = = A
- Note value = = = B
- Number of maps = = 3
Customizing Java Annotation, reading annotation values