`
djun100
  • 浏览: 168761 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Java 自定义Annotation

 
阅读更多
很实用几个例子,在Java开发中,灵活运用可以解决很多问题,比如说持久化实现,还可以配合Struts拦截器解决权限问题,可以控制到方法。
Java代码收藏代码
  1. packagecn.annotation;
  2. /**
  3. *DefineAnnotationkeywordsis'@interface'soasclass
  4. *Ifyoudon'tsetdefaultvaluetoattributeoffield
  5. *whenyouquotedefinedAnnotation,youshouldclearanddefine
  6. *attributesthatyoudefineintheAnnotation
  7. *@authorAdministrator
  8. *@since2011/12/21
  9. *
  10. */
  11. @interfaceMyannotation{
  12. publicStringkey();
  13. publicStringtableName();
  14. publicintyear();
  15. }
  16. publicclassAnnotationDemo01{
  17. @Myannotation(key="yangyang",tableName="talbe",year=23)
  18. publicvoidgetInfo(){
  19. System.out.println("自定义Annotation");
  20. }
  21. publicstaticvoidmain(Stringargs[]){
  22. AnnotationDemo01a1=newAnnotationDemo01();
  23. a1.getInfo();
  24. }
  25. }


Java代码收藏代码
  1. packagecn.annotation;
  2. /**
  3. *DefineAnnotationkeywordsis'@interface'soasclass
  4. *Ifyoudon'tsetdefaultvaluetoattributeoffield
  5. *whenyouquotedefinedAnnotation,youshouldclearanddefine
  6. *attributesthatyoudefineintheAnnotation
  7. *@authorAdministrator
  8. *
  9. */
  10. @interfaceMyannotation02{
  11. publicStringkey()default"key";
  12. publicStringtableName()default"table";
  13. publicintyear()default0;
  14. }
  15. publicclassAnnotationDemo02{
  16. @Myannotation02
  17. publicvoidprintInfo(){
  18. System.out.println("自定Annotation,并设置默认值!");
  19. }
  20. publicstaticvoidmain(String[]args){
  21. AnnotationDemo02a2=newAnnotationDemo02();
  22. a2.printInfo();
  23. }
  24. }

Java代码收藏代码
  1. packagecn.annotation;
  2. importjava.lang.annotation.Retention;
  3. importjava.lang.annotation.RetentionPolicy;
  4. /**
  5. *Annoation范围:
  6. *RetentionPolicy.SOURCE此Annotation信息只保留在程序源文件中(.java)
  7. *RetentionPolicy.CLASS此Annotation信息保留在源程序(.java)和编译之后的类文件(.class)中,不加载到Jvm中。默认方式
  8. *RetentionPolicy.RUNTIME此Annotation信息保留在源程序(.java)和编译之后的类文件(.class)中,运行时加载到Jvm中
  9. *@authorAdministrator
  10. *
  11. */
  12. @Retention(value=RetentionPolicy.RUNTIME)
  13. @interfaceMyannotation04{
  14. publicStringname()default"yangyang";
  15. }


Java代码收藏代码
  1. packagecn.annotation;
  2. importjava.lang.annotation.Annotation;
  3. importjava.lang.annotation.Retention;
  4. importjava.lang.annotation.RetentionPolicy;
  5. importjava.lang.reflect.Method;
  6. /**
  7. *通过JAVA反射取Annotation
  8. *只有执行时才会加载到Jvm中,才可以取得Annotation信息
  9. *@authorAdministrator
  10. *
  11. */
  12. @Retention(value=RetentionPolicy.RUNTIME)
  13. @interfaceMyannotation05{
  14. publicStringname()default"name";
  15. publicStringsex()default"男";
  16. publicintage()default20;
  17. }
  18. classSimple{
  19. publicvoidprintInfo(){
  20. System.out.println("***");
  21. }
  22. }
  23. classSimpleBeanextendsSimple{
  24. @SuppressWarnings("Unchecked")
  25. @Deprecated
  26. @Override
  27. @Myannotation05(name="张三",sex="男",age=25)
  28. publicvoidprintInfo(){
  29. System.out.println("获取Annotaion信息");
  30. }
  31. }
  32. publicclassAnnotationDemo04{
  33. publicstaticvoidmain(String[]args){
  34. Class<SimpleBean>classz=SimpleBean.class;
  35. try{
  36. Methodmethod=classz.getMethod("printInfo",null);
  37. if(method.isAnnotationPresent(Myannotation05.class)){
  38. Myannotation05info=method.getAnnotation(Myannotation05.class);
  39. System.out.println("姓名:"+info.name()+"、性别:"+info.sex()+"、年龄:"+info.age());
  40. }
  41. }catch(Exceptione){
  42. System.out.println(e.getMessage());
  43. }
  44. }
  45. }
  46. 转自:http://yangyangmyself.iteye.com/blog/1320032
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics