Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96962
  • 博文数量: 73
  • 博客积分: 3971
  • 博客等级: 中校
  • 技术积分: 875
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-09 20:57
文章分类

全部博文(73)

文章存档

2008年(73)

我的朋友
最近访客

分类:

2008-05-14 12:34:17

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

 

阅读(211) | 评论(0) | 转发(0) |
0

上一篇:Spring AOP入门

下一篇:Spring 框架简介

给主人留下些什么吧!~~