駱昊的Java空間

          傳道、授業、解惑,何有于我哉! Java SE / Java ME / Java EE / C / C++ / C#

          Spring AOP之Introduction(@DeclareParents)簡介

          Spring的文檔上對Introduction這個概念和相關的注解@DeclareParents作了如下介紹:

          Introductions (known as inter-type declarations in AspectJ) enable an aspect to declare that advised objects implement a given interface, and to provide an implementation of that interface on behalf of those objects.
          An introduction is made using the @DeclareParents annotation. This annotation is used to declare that matching types have a new parent (hence the name).


          在這段介紹之后還給出了一個例子,對于初學者要理解這段話以及后面的例子還是蠻困難的,因此下面用一個簡單的例子告訴大家什么是Introduction以及如何使用@DeclareParents注解。

          對于Introduction這個詞,個人認為理解成引入是最合適的,其目標是對于一個已有的類引入新的接口(有人可能會問:有什么用呢?簡單的說,你可以把當前對象轉型成另一個對象,那么很顯然,你就可以調用另一個對象的方法了),看一個例子就全明白了。

          假設已經有一個UserService類提供了保存User對象的服務,但是現在想增加對User進行驗證的功能,只對通過驗證的User提供保存服務,在不修改UserService類代碼的前提下就可以通過Introduction來解決。

          首先定義一個Verifier接口,里面定義了進行驗證的方法validate(),如下所示:
          package com.jackfrued.aop;

          import com.jackfrued.models.User;

          public interface Verifier {

              
          public boolean validate(User user);
          }

          接下來給出該接口的一個實現類BasicVerifier,如下所示:
          package com.jackfrued.aop;

          import com.jackfrued.models.User;

          public class BasicVerifier implements Verifier {

              @Override
              
          public boolean validate(User user) {
                  
          if(user.getUsername().equals("jack"&& user.getPassword().equals("1234")) {
                      
          return true;
                  }

                  
          return false;
              }

          }


          如何才能為UserService類增加驗證User的功能呢,如下所示定義Aspect:
          package com.jackfrued.aop;

          import org.aspectj.lang.annotation.Aspect;
          import org.aspectj.lang.annotation.DeclareParents;
          import org.springframework.stereotype.Component;

          @Aspect
          @Component
          public class MyAspect {
              @DeclareParents(value
          ="com.tsinghuait.services.UserService"
                      defaultImpl
          =com.tsinghuait.aop.BasicVerifier.class)
              
          public Verifier verifer;
          }
          接下來就可以將UserService對象轉型為Verifier對象并對用戶進行驗證了,如下所示:
          package com.jackfrued.main;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import com.jackfrued.aop.Verifier;
          import com.jackfrued.models.User;
          import com.jackfrued.services.Service;

          class Test {

              
          public static void main(String[] args) {
                  User user1 
          = new User();
                  user1.setUsername(
          "abc");
                  user1.setPassword(
          "def");
                  
                  ApplicationContext factory 
          = new ClassPathXmlApplicationContext("config.xml");
                  Service s 
          = (Service) factory.getBean("service");
                  Verifier v 
          = (Verifier) s;
                  
          if(v.validate(user1) {
                      System.out.println(
          "驗證成功");
                      s.serve(user1);
                  }

                  
              }

          }

          這樣,上面代碼中的user1是不會被服務的,當然是因為沒有通過驗證啦!

          這樣一說,是不是大概明白什么是Introduction了呢,其實@DeclareParents用起來也很簡單吧!

          至于配置文件和其他內容請參考完整源代碼:/Files/jackfrued/Spring-introduction.rar

          posted on 2010-02-27 14:09 駱昊 閱讀(4811) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2010年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28123456
          78910111213

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 麟游县| 三穗县| 兴国县| 秦皇岛市| 防城港市| 乐平市| 林周县| 峨山| 合肥市| 仪陇县| 白沙| 新巴尔虎右旗| 西丰县| 招远市| 莱阳市| 张家港市| 塔河县| 英超| 同心县| 黔江区| 阳新县| 长海县| 天镇县| 九龙县| 龙州县| 吕梁市| 龙胜| 上栗县| 台东县| 桐柏县| 米林县| 济阳县| 凭祥市| 买车| 襄垣县| 沂水县| 新乡市| 丽江市| 应用必备| 黑山县| 昆山市|