首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Annotation详细引见

2013-03-10 
Annotation详细介绍转自:http://snkcxy.iteye.com/blog/1821512如约所致~ Annotation详细介绍博客分类:?ja

Annotation详细介绍
转自:http://snkcxy.iteye.com/blog/1821512如约所致~ Annotation详细介绍博客分类:

    ?javaJava代码??Annotation详细引见
    1. package?com.cxy.annotation;??
    2. ??
    3. import?java.lang.annotation.ElementType;??
    4. import?java.lang.annotation.Retention;??
    5. import?java.lang.annotation.RetentionPolicy;??
    6. import?java.lang.annotation.Target;??
    7. import?java.lang.reflect.Method;??
    8. ??
    9. /**?
    10. ?*?@author?cxy?
    11. ?*/??
    12. public?class?AnnotationDetailTest??
    13. {??
    14. ????public?static?void?main(String[]?args)?throws?Exception??
    15. ????{??
    16. ????????CxyLogAp.Processor(Aclass.class);??
    17. ????}??
    18. }??
    19. ??
    20. /**?
    21. ?*?一个测试用的类,这个类的method1和method3被@CxyLog标记?
    22. ?*/??
    23. class?Aclass??
    24. {??
    25. ????@CxyLog??
    26. ????public?static?void?method1(){System.out.println("method1执行");}??
    27. ????public?static?void?method2(){System.out.println("method2执行");}??
    28. ????@CxyLog??
    29. ????public?static?void?method3(){System.out.println("method3执行");}??
    30. ????public?static?void?method4(){System.out.println("method4执行");}??
    31. }??
    32. ??
    33. /**?
    34. ?*?一个简单的annotation,打上这个标记的我们就给这个方法加入日志打印功能?
    35. ?*?1.@Retention(RetentionPolicy.RUNTIME)?解释:保留注释到程序运行?
    36. ?*?2.@Target(ElementType.METHOD)?解释:这个annotation是标记在方法上的?
    37. ?*/??
    38. @Retention(RetentionPolicy.RUNTIME)???
    39. @Target(ElementType.METHOD)???
    40. @interface?CxyLog{}???
    41. ??
    42. ??
    43. /**这个类是@CxyLog的annotation处理器?
    44. ?*/??
    45. class?CxyLogAp??
    46. {??
    47. ????//处理器?处理@CxyLog标记??
    48. ????public?static?void?Processor(Class?clz)?throws?Exception??
    49. ????{??
    50. ????????for(Method?m:clz.getMethods())??
    51. ????????{??
    52. ????????????//如果当前访问的方法打上了@CxyLog标识,那么就加入打印语句??
    53. ????????????if(m.isAnnotationPresent(CxyLog.class))??
    54. ????????????{??
    55. ????????????????m.invoke(null);??
    56. ????????????????System.out.println("日志:"+clz+"."+m.getName()+"执行!");??
    57. ????????????}else??
    58. ????????????{??
    59. ????????????????//因为他会执行所有的方法,包括父类的很多方法,所以这里我们限定一下只做我们定义的method开头的方法??
    60. ????????????????if(m.getName().contains("method"))??
    61. ????????????????{??
    62. ????????????????????m.invoke(null);??
    63. ????????????????}??
    64. ????????????}??
    65. ????????}??
    66. ????}??
    67. }??

    ?

    结果图:


    ? ??Annotation详细引见
    ?

    例二:

    Java代码??Annotation详细引见
    1. package?com.cxy.annotation;??
    2. ??
    3. import?java.lang.annotation.Annotation;??
    4. import?java.lang.annotation.ElementType;??
    5. import?java.lang.annotation.Retention;??
    6. import?java.lang.annotation.RetentionPolicy;??
    7. import?java.lang.annotation.Target;??
    8. import?java.lang.reflect.Method;??
    9. ??
    10. /**?
    11. ?*?@author?cxy?
    12. ?*/??
    13. public?class?AnnotationDetailTest1??
    14. {??
    15. ????public?static?void?main(String[]?args)?throws?Exception??
    16. ????{??
    17. ????????CxyAction.commonAuthoritymethod();??
    18. ????????CxyAction.advanceAuthoritymethod();??
    19. ????????System.out.println("=============================");??
    20. ????????//以下面的两种方式执行这两个方法??
    21. ????????CxyAuthorityAp.Processor(CxyAction.class,?"commonAuthoritymethod");??
    22. ????????System.out.println("------------------------------");??
    23. ????????CxyAuthorityAp.Processor(CxyAction.class,?"advanceAuthoritymethod");??
    24. ????}??
    25. }??
    26. ??
    27. /**?测试目标类,提供了两个不同权限的方法?
    28. ?*/??
    29. class?CxyAction??
    30. {??
    31. ????public?static?CxyBo?bo=new?CxyCommonBo();?//初始时候装配的是普通权限的bo??
    32. ??????
    33. ????//普通权限就能访问的方法??
    34. ????@CxyAuthority(authority="c",boName=CxyCommonBo.class)??
    35. ????public?static?void?commonAuthoritymethod()??
    36. ????{??
    37. ????????bo.doSomething();??
    38. ????}???
    39. ??????
    40. ????//高级权限能访问的方法???
    41. ????@CxyAuthority(authority="a",boName=CxyAdvancedBo.class)??
    42. ????public?static?void?advanceAuthoritymethod()??
    43. ????{??
    44. ????????bo.doSomething();??
    45. ????}???
    46. }??
    47. ??
    48. /**?一个接口为了演示动态装配而生?
    49. ?*/??
    50. interface?CxyBo??
    51. {??
    52. ????public?void?doSomething();??
    53. }??
    54. ??
    55. /**?普通权限的bo?
    56. ?*/??
    57. class?CxyCommonBo?implements?CxyBo??
    58. {??
    59. ????//普通业务逻辑??
    60. ????@Override??
    61. ????public?void?doSomething()??
    62. ????{??
    63. ????????System.out.println("执行:普通权限处理方法");??
    64. ????}??
    65. }??
    66. ??
    67. /**?高级权限的bo?
    68. ?*/??
    69. class?CxyAdvancedBo?implements?CxyBo??
    70. {??
    71. ????//高级业务逻辑??
    72. ????@Override??
    73. ????public?void?doSomething()??
    74. ????{??
    75. ????????System.out.println("执行:高级权限处理方法");??
    76. ????}??
    77. }??
    78. ??
    79. //权限annotation,根据这个标记的authority值来判断装配哪个业务逻辑类??
    80. @Retention(RetentionPolicy.RUNTIME)???
    81. @Target(ElementType.METHOD)???
    82. @interface?CxyAuthority??
    83. {??
    84. ????String?authority();?//代表权限??
    85. ????Class?boName();?//代表这个权限用什么bo去处理??
    86. }??
    87. ??
    88. /**这个类是@CxyAuthority的annotation处理器?
    89. ?*/??
    90. class?CxyAuthorityAp??
    91. {??
    92. ????//处理器?处理@CxyAuthority标记??
    93. ????public?static?void?Processor(Class?clz,String?methodName)?throws?Exception??
    94. ????{??
    95. ????????Method?m=clz.getMethod(methodName,?null);?//获得指定类的指定方法??
    96. ????????Annotation[]?aArray=m.getAnnotations();?//获得方法所有的annotation??
    97. ????????for(Annotation?one:aArray)??
    98. ????????{??
    99. ????????????if(one?instanceof?CxyAuthority)??
    100. ????????????{??
    101. ????????????????CxyBo?tempBo=(CxyBo)?((CxyAuthority)one).boName().newInstance();??
    102. ????????????????if("c".equals(((CxyAuthority)one).authority()))??
    103. ????????????????{??
    104. ????????????????????System.out.println("装配信息:普通权限bo装配成功");??
    105. ????????????????????//这里还可以做一些权限控制的操作??
    106. ????????????????}else?if("a".equals(((CxyAuthority)one).authority()))??
    107. ????????????????{??
    108. ????????????????????System.out.println("装配信息:高级权限bo装配成功");??
    109. ????????????????????//这里还可以做一些权限控制的操作??
    110. ????????????????}??
    111. ????????????????tempBo.doSomething();??
    112. ????????????}??
    113. ????????}??
    114. ????}??
    115. }??

    ?结果图:


    ? ??Annotation详细引见
    ?

    ?

    声明:

    1.原创文章,转载请标明并加本文连接。

    2.文章反映个人愚见,如有异议欢迎讨论指正

热点排行