夢幻之旅

          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 閱讀(1121) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 无极县| 治多县| 上思县| 板桥市| 房山区| 桂林市| 江山市| 巴林左旗| 三都| 文安县| 钟祥市| 三穗县| 阿瓦提县| 汉川市| 衡阳县| 陆川县| 横峰县| 凯里市| 安仁县| 安丘市| 绍兴市| 漳平市| 高淳县| 涿鹿县| 凭祥市| 冷水江市| 烟台市| 太谷县| 安多县| 江都市| 酉阳| 江陵县| 棋牌| 宝兴县| 大庆市| 玉林市| 双柏县| 昂仁县| 海伦市| 得荣县| 湾仔区|