Spring中基于aop命名空间的AOP

標簽: 瀏覽(185)  日期:2008-09-07

本文地址:http://www.blogjava.net/cmzy/archive/2008/08/23/223870.html
下篇地址:Spring中基于aop命名空间的AOP 二(声明一个切面、切入点和通知)

    在某些时候,我们工程中使用的JDK 不一定就是1.5 以上,也就是说可能不支持Annotation 注解,这时自然也就不能使用@AspectJ 注解驱动的AOP 了,那么如果我们仍然想使用AspectJ 灵活的切入点表达式,那么该如何呢?Spring 为我们提供了基于xml schematic 的aop 命名空间,它的使用方式和@AspectJ 注解类似,不同的是配置信息从注解中转移到了Spring 配置文件中。在这里,我们将详细介绍如何使用Spring 提供的<aop:config/> 标签来配置Spring AOP 。


1 、一点准备工作和一个例子

    使用<aop:config/> 标签,需要给Spring 配置文件中引入基于xml schema 的Spring AOP 命名空间。完成后的Spring 配置文件如下(在该节,所有例程的配置文件中添加了Spring AOP 命名空间,除非特殊情况外,为了节约空间,这部分将在给出的代码中省略),粗体内容即为我们需要添加的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  7.               http://www.springframework.org/schema/aop    
  8.               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd >  
  9. ………… Spring配置信息   
  10. </beans>  

    关于aop命名空间的标签,我们前面使用过的有<aop:aspectj-autoproxy/>,在这一节,我们将以<aop:config/>标签作为重点。事实上,我们在这一节介绍的所有标签都是该标签的子标签。


   下面有一个例程来直观的展示如何使用<aop:config/>标签来配置Spring AOP(完整代码见例程4.15)。在例子中,我们使用<aop:config/>配置一个切面并拦截目标对象Peoples的SayHello()方法,在它执行前输出提示信息。
首先创建工程AOP_Test4.15,添加Spring IoC和Spring AOP库后,创建aop.test包,新建目标类People,代码如下:

  1. package aop.test;   
  2.   
  3. /**  
  4.  * 该类将作为目标对象对应的类。  
  5.  * @author zhangyong  
  6.  * */  
  7. public class People{   
  8.   
  9.         public String SayHello(String str){   
  10.                 System.out.println(this.getClass().getName()+ "说:"+str);   
  11.                 return str;   
  12.         }   
  13. }   

    修改Spring xml配置文件,将该类注册为一个受管Bean:

  1. <bean id="TestBean" class="aop.test.People" />  

    创建含有main()方法的测试类TestMain,从Spring IoC容器中获取Peoples对象,并调用其SayHello()方法,代码如下:

  1. package aop.test;   
  2.   
  3. // import省略   
  4. public class TestMain {   
  5.         public static void main(String[] args) {   
  6.                 // 实例化Spring IoC容器   
  7.                 ApplicationContext ac = new ClassPathXmlApplicationContext(   
  8.                                 "applicationContext.xml");   
  9.                 // 获取受管Bean的实例   
  10.                 People p = (People) ac.getBean("TestBean");   
  11.                 p.SayHello("传入的参数值");   
  12.         }   
  13. }   

   创建MyAspect类,添加一个beforeAdvice()方法作为前置通知方法,代码如下:

  1. package aop.test;   
  2.   
  3. import org.aspectj.lang.JoinPoint;   
  4.   
  5. public class MyAspect {   
  6.            
  7.         public void beforeAdvice(JoinPoint point) {   
  8.             System.out.println("前置通知被触发:" +    
  9.                                 point.getTarget().getClass().getName()+    
  10.                                 "将要" + point.getSignature().getName());   
  11.         }   
  12. }   

    修改xml配置文件,为其添加aop命名空间,并把MyAspect注册为一个受管Bean,作为我们下面定义切面的backing bean。代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  7.                http://www.springframework.org/schema/aop    
  8. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  9.   
  10.         <bean id="MyAspect" class="aop.test.MyAspect" />  
  11.         <bean id="TestBean" class="aop.test.People" />  
  12.            
  13.         <aop:config proxy-target-class="true">  
  14.                 <aop:aspect ref="MyAspect" order="0" id="Test">  
  15.                         <aop:pointcut id="testPointcut"  
  16.                                 expression="execution(* aop..*(..))" />  
  17.                         <aop:before pointcut-ref="testPointcut"  
  18.                                 method="beforeAdvice" />  
  19.                 </aop:aspect>  
  20.         </aop:config>  
  21. </beans>  

    运行主类,输出如下:

例程4.15输出结果

例程4.15输出结果

 

2、声明一个切面
      在基于AOP命名空间的Spring AOP中,要声明一个切面,需要使用<aop:config/>的子标签<aop:aspect>。<aop:aspect>标签有一个ref属性必须被赋值,它用于指定和该切面关联的受管Bean(backing bean,以后我们都将使用Backing Bean来称呼这样的Bean)。正如下例所示,该Bean对应的java类是一个普通的java类,在该类中定义了切面的通知方法。此外,<aop:aspect>标签还有两个可选的order属性和id属性,order属性用于指定该切面的加载顺序,id属性用于标识该切面。范例如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans ……>  
  3.         <bean id="MyAspect" class="aop.test.MyAspect" />  
  4.         <aop:config proxy-target-class="true">  
  5.                 <aop:aspect ref="MyAspect" order="1" id="TestAspectName">  
  6.                         ……切面其他配置   
  7.                 </aop:aspect>  
  8.         </aop:config>  
  9. ……其他配置   
  10. </beans>    

 

3、声明一个切入点
      要声明一个切入点,可以使用<aop:aspect>的子标签<aop:pointcut>,在Spring2.5中它有两个属性id和expression,分别用于标示该切入点和设定该切入点表达式。例如:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans ……>  
  3.     <bean id="MyAspect" class="aop.test.MyAspect"/>  
  4.     <aop:config proxy-target-class="true">  
  5.         <aop:aspect ref="MyAspect" order="1" id=”TestAspectName”>  
  6.               <aop:pointcut id="test"  
  7.                 expression="execution(* aop.test.TestBean.*(..))"/>  
  8.               <aop:before pointcut="aop.test.MyAspect.Pointcut1()"    
  9.                                method="beforeAdvice" />  
  10.         </aop:aspect>  
  11.     </aop:config>  
  12. ……其他配置   
  13. </beans>  

 


<aop:pointcut>标签的expression属性使用前面介绍的切入点表达式语言,也就是说支持AspectJ切入点表达式。但是由于xml对"&&"、"||"、"!"等逻辑运算符不友好,@AspectJ切入点表达式语言中使用的这些逻辑运算符在xml配置中需要分别用"and"、"or"和"not"来代替。
有时候,我们也需要在xml中使用@Pointcut注解声明的切入点,那么该如何呢?大家可能记得,我们可以在切入点表达式中可以引用另一个切入点。对了,就在这里,我们使用该特性可以完成这个任务,如下:

  1. <aop:pointcut id="test"   expression="aop.test.MyAspect.Pointcut1()" />  

 

注意:这里我们必须使用全路径来标示引用的切入点。

4、 声明一个通知
      和@AspectJ一样,基于AOP命名空间的配置也可以定义五种通知类型,并且使用方式和特性类似。与@AspectJ不同的是,配置信息从Annotation中转移到了xml配置文件。
    1)、前置通知
    声明一个前置通知可以使用<aop:aspect>的子标签<aop:before/>。该标签的属性说明如下表:

<aop:before/>标签属性说明

属性

说明

pointcut

指定该通知的内置切入点

pointcut-ref

通过 id 引用已定义的切入点

method

指定通知对应的方法,该方法必须已在切面的 backing bean 中被声明

arg-names

通过方法的参数名字来匹配切入点参数

      对于一个通知来说,切入点和对应的通知方法是必须的。也就是说,在这些属性中,method属性是必须的,我们必须要给通知指定一个对应的方法;pointcut属性和pointcut-ref必须有一个被指定,以此确定该通知的切入点。范例如下:

  1. <aop:aspect ref="MyAspect" order="0" id="Test">  
  2.     <aop:pointcut id="testPointcut"  
  3.         expression="execution(* aop.test.TestBean.*(..))"/>  
  4.     <aop:before pointcut-ref="testPointcut" method="beforeAdvice"/>  
  5. </aop:aspect>  

 

     2)、 后置通知
     声明一个后置通知使用<aop:after/>标签,它的属性等和<aop:before/>标签类似,下面是范例:

  1. <aop:aspect ref="MyAspect" order="0" id="Test">  
  2.     <aop:pointcut id="testPointcut"  
  3.         expression="execution(* aop.test.TestBean.*(..))" />  
  4.     <aop:after  pointcut-ref="testPointcut" method="AfterAdvice"/>  
  5. </aop:aspect>  

 

     3)、 返回后通知
      <aop:after-returning/>标签可以声明一个返回后通知,该标签的属性和<aop:before/>相比它多了一个returning属性。该属性的意义类似于@AfterReturning注解的returning属性,用于将链接点的返回值传给通知方法。用法如下:

  1. <aop:aspect ref="MyAspect" order="0" id="Test">  
  2.     <aop:pointcut id="testPointcut"  
  3.         expression="execution(* aop.test.TestBean.*(..))" />  
  4.     <aop:after-returning pointcut-ref="testPointcut"  
  5.         method="AfterReturnAdvice" returning="reVlue" />  
  6. </aop:aspect>  

 

       4)、 异常通知
        声明一个异常通知使用<aop:after-throwing />标签,它有一个类似于throwing属性又来指定该通知匹配的异常类型。用法如下:

  1. <aop:aspect ref="MyAspect" order="0" id="Test">  
  2.      <aop:pointcut id="testPointcut"  
  3.         expression="execution(* aop.test.TestBean.*(..))" />  
  4.      <aop:after-throwing pointcut-ref="testPointcut"  
  5.         method="afterThrowingAdvice" throwing="throwable" />  
  6. </aop:aspect>  

 

      5)、 环绕通知
      环绕通知是所有通知中功能最强大的通知,用<aop:around/>标签来声明。用法如下:

  1. <aop:aspect ref="MyAspect" order="0" id="Test">  
  2.     <aop:pointcut id="testPointcut"  
  3.         expression="execution(* aop.test.TestBean.*(..))" />  
  4.         <aop:around pointcut-ref="testPointcut" method="aroundAdvice"/>  
  5. </aop:aspect>  




(完)




-------------------------------------------------
上一篇:EXT2.0 简明教程! 下一篇:Maven入门--概念与实例



  
Are you Bot? How you know that?ofcz no.