夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          本練習有三個目標:
          1.如何定義注釋類型
          2.如何使用注釋類型
          3.如何讓注釋影響程序運行

          一.如何定義注釋類型
          package org.test.spring.annotation;

          import java.lang.annotation.ElementType;
          import java.lang.annotation.Retention;
          import java.lang.annotation.RetentionPolicy;
          import java.lang.annotation.Target;

          /**
           * <p>
           * 定義一個Person類專用的注釋
           * </p>
           * 元注釋Retention用于指定此注釋類型的注釋要保留多久, 元注釋Target用于指定此注釋類型的注釋適用的程序元素的種類,
           * 
           * 
          @author Huy Vanpon
           
          */
          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.METHOD)
          public @interface Annot4Person
          {
              String name() 
          default "惠萬鵬";
              
              
          int age() default 25;
              
              String gender() 
          default "";
          }


          二.2.如何使用注釋類型
          package org.test.spring.annotation;

          /**
           * <p>
           * 本類用于定義人類,包含一個自我介紹的方法
           * </p>
           * 
           * 
          @author Huy Vanpon
           
          */
          public class Person
          {
              @Annot4Person(name 
          = "何潔", gender = "", age = 16)
              
          public void introductionMyself(String name, String gender, int age)
              {
                  StringBuffer sb 
          = new StringBuffer();
                  sb.append(
          "嗨,大家好,我叫");
                  sb.append(name);
                  sb.append(
          ",今年");
                  sb.append(age);
                  sb.append(
          "歲,是一個充滿陽光的");
                  sb.append(gender);
                  sb.append(
          "孩.");
                  System.out.println(sb.toString());
              }
          }


          三.如何讓注釋影響程序運行
          package org.test.spring.annotation;

          import java.lang.reflect.Method;

          /**
           * <p>
           * 本用利JAVA的反射機制,讓注釋影響程序的運行
           * </p>
           * 
           * 
          @author Huy Vanpon
           
          */
          public class AnnotDisturber
          {
              
          public void testAnnot4Person()
              {
                  Class
          <Person> clazz = Person.class;
                  
          try
                  {
                      
          //通過方法名和入參確定方法
                      Method method = clazz.getDeclaredMethod("introductionMyself",
                              String.
          class, String.classint.class);
                      
          if (method == null)
                      {
                          
          return;
                      }
                      
          //從方法取得注釋
                      Annot4Person annotation = method.getAnnotation(Annot4Person.class);
                      
          if (annotation == null)
                      {
                          
          return;
                      }
                      
          //調用這個方法
                      method.invoke(new Person(), annotation.name(), annotation.gender(),
                              annotation.age());
                  }
                  
          catch (Exception e)
                  {
                      e.printStackTrace();
                  }
              }
              
              
          public static void main(String[] args)
              {
                  AnnotDisturber tt 
          = new AnnotDisturber();
                  tt.testAnnot4Person();
              }
          }


          posted on 2008-07-25 11:48 HUIKK 閱讀(1120) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 孟津县| 德化县| 洞头县| 建水县| 囊谦县| 揭东县| 肇源县| 宁国市| 稻城县| 紫云| 南雄市| 河北区| 和顺县| 灵川县| 赫章县| 仲巴县| 龙胜| 伊金霍洛旗| 尖扎县| 孟村| 湖州市| 庐江县| 茶陵县| 牡丹江市| 清水河县| 东乡| 娄烦县| 海晏县| 仁化县| 红桥区| 兴仁县| 林甸县| 从江县| 孝义市| 西林县| 平谷区| 电白县| 武胜县| 突泉县| 普兰店市| 铅山县|