Spring AOP实际应用一例

    WEB开发中,用户对网页的访问权限检查是一个重要的环节。以STRUST为例,我们需要在Actionexcute方法中编写相关的代码(一般是调用基类的函数),也很显然,在每个Action中这是一种重复劳动。   
   
如果我们在excute运行之前,能够自动去调用基类的权限检查函数,这无疑是个好的解决办法。AOP就为我们提供了这样一种解决方法。   
         
下面以一个简化的实例介绍实现的办法。   
首先我们做一个接口:   

  1. public   interface   CheckInterface   {      
  2.       public   abstract   void   check(String   name);      
  3.       public   abstract   void   excute(String   name);      
  4.   }      


  再做一个基类:  

  1. public   abstract   class   BaseClass   implements   CheckInterface   {      
  2. public   BaseClass()   {      
  3. }      
  4. public   void   check(String   name){      
  5. if   (name.equals("supervisor"))      
  6. System.out.println("Check   Pass!!");      
  7. else   {      
  8. System.out.println("No   access   privilege!   Please   do   sth.   else!");      
  9. }      
  10. }      
  11. }      

 

再做一个测试类:   

  1. public   class   ExcuteClass   extends   BaseClass   {      
  2.  public   ExcuteClass()   {      
  3.  }      
  4.       
  5.  public   void   excute(String   name){      
  6.  System.out.println("Excute   here!"+name);      
  7.  }      
  8.  }    

 

 好了,下面做一个通知类(Advice):   

java 代码
  1. import   org.springframework.aop.MethodBeforeAdvice;      
  2.  import   java.lang.reflect.Method;      
  3.  import   org.apache.log4j.Logger;      
  4.       
  5.  public   class   BeforeAdvisor   implements   MethodBeforeAdvice   {      
  6.  private   static   Logger   logger=Logger.getLogger(BeforeAdvisor.class);      
  7.  public   void   before(Method  m,Object[] args,   Object   target)   throws   Throwable   {      
  8.  if   (target   instanceof   CheckInterface){      
  9.  logger.debug("Is   Instanceof   CheckInterface!!!");      
  10.  CheckInterface   ci=(CheckInterface)target;      
  11.  ci.check((String)args[0]);      
  12.  }      
  13.  }      
  14.  }      

 

     其中重要的before方法的参数:Object   target传入的通知的对象(即测试类的接口),Method   m,  Object[]   args

别是该对象被调用的方法和参数。 

我们再来作spring   bean定义xml文件:    

xml 代码
  1. xml   version="1.0"   encoding="UTF-8"?>      
  2.  >      
  3.  <beans>      
  4.  <description>Spring   Quick   Startdescription>      
  5.  <bean   id="MyAdvisor"   class="com.wysm.netstar.test.springaop.BeforeAdvisor"/>      
  6.       
  7.  <bean   id="myPointcutAdvisor2"   class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">      
  8.  <property   name="advice">      
  9.  <ref   local="MyAdvisor"   />      
  10.  property>      
  11.  <property   name="patterns">      
  12.  <list>      
  13.  <value>.*excute.*value>      
  14.  list>      
  15.  property>      
  16.  bean>      
  17.       
  18.  <bean   id="checkInterface"   class="com.wysm.netstar.test.springaop.ExcuteClass"/>      
  19.       
  20.  <bean   id="myCheckClass"   class="org.springframework.aop.framework.ProxyFactoryBean">      
  21.  <property   name="proxyInterfaces">      
  22.  <value>com.wysm.netstar.test.springaop.CheckInterfacevalue>      
  23.  property>      
  24.  <property   name="target">      
  25.  <ref   local="checkInterface"   />      
  26.  property>      
  27.  <property   name="interceptorNames">      
  28.  <value>myPointcutAdvisor2value>      
  29.  property>      
  30.  bean>      
  31.       
  32.  beans>      
     
    最后是测试类:   

  我们再来作spring   bean定义xml文件:  这个定义文件指明了ExcuteClass为监视对象,它的excute方法被执行的时候,BeforeAdvisor将被调用。   

java 代码

 

  1.  
  2.  import   junit.framework.TestCase;      
  3.  import   org.springframework.context.ApplicationContext;      
  4.  import   org.springframework.context.support.FileSystemXmlApplicationContext;      
  5.       
  6.  public   class   SpringTestCase2   extends   TestCase   {      
  7.  CheckInterface   test=null;      
  8.       
  9.  protected   void   setUp()   throws   Exception   {      
  10.  super.setUp();      
  11.  ApplicationContext   ctx=new   FileSystemXmlApplicationContext("src/com/wysm/netstar/test/springaop/aoptest.xml");      
  12.  test   =   (CheckInterface)   ctx.getBean("myCheckClass");      
  13.  }      
  14.       
  15.  protected   void   tearDown()   throws   Exception   {      
  16.  super.tearDown();      
  17.  }      
  18.  public   void   testExcute(){      
  19.  test.excute("supervisor");      
  20.  }      
  21.  }     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值