`

Spring中基于aop命名空间的AOP

阅读更多

(残梦追月原创,转载请注明)

本文地址:http://www.blogjava.net/cmzy/archive/2008/08/23/223870.html

    在某些时候,我们工程中使用的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/>标签属性说明

 

margin-top: 0cm; margin-rig

分享到:
评论

相关推荐

    springAOP demo 带错误解决文档

    1 需要在xml文件中加入命名空间 可以在spring-framework-4.0.6.RELEASE\docs\spring-framework-reference\html 中搜索关键字查找配置 2 加入需要的jar包(依赖包) 由于没有引入 spring-aop-4.0.6.RELEASE.jar 引起的...

    aop-log:项目正式命名为aop-log,基于Spring AOP,ThreadLocal实现方法埋点,埋点信息记录和自定义收集

    AopLogAopLog是基于SpringAop和ThreadLocal实现的一个对请求方法埋点记录与处理的日志工具包。设计目的和场景:使用Spring Aop拦截程序,基本上都是同一个小异,不想日后每个项目都柏林都写一份这样的Aop拦截处理...

    Spring添加声明式事务.docx

    一、前言 Spring提供了声明式事务处理机制,它基于AOP实现,无须编写任何事务管理代码,所有的工作全在...为业务方法配置事务切面,需要用到tx和aop两个命名空间下的标签,先在Spring配置文件中导入这两个命名空间。

    spring 注解介绍

    Spring框架从创建伊始就致力于为复杂问题提供...从此它便深深植根于核心Spring框架(aop、context、jee、jms、 lang、tx和util命名空间)、Spring Portfolio项目(例如Spring Security)和非Spring项目中(例如CXF)。

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    spring.net中文手册在线版

    17.4.命名空间 17.5.数据访问的方式 17.6.AdoTemplate简介 17.6.1.执行回调 17.6.2.在.NET 2.0中执行回调 17.6.3. .NET 1.1 17.6.4.AdoTemplate方法指南 17.7.异常翻译 17.8.参数管理 17.8.1. IDbParametersBuilder ...

    spring框架api中文版.zip(spring开发手册)

    依赖关系管理和命名约定 Spring依赖和依靠弹簧 Maven依赖管理 艾薇依赖管理 1.3.2。 日志 不使用通用日志 使用SLF4J 使用Log4j 二世。 什么是新的在春季3 2。 新特性和增强功能在Spring框架3.0 2.1。 Java 5 2.2。 ...

    spring2.0声明式事务

    iii. 修改spring配置文件,增加常用命名空间 ”1.0” encoding=”utf-8”?&gt; ”http://www.springframework.org/schema/beans” xmlns:xsi=”http://w3.org/2001/XMLSchema-instance” xmlns:aop=” ...

    spring学习笔记(有代码有注解解释)

    Spring 的 p 命名空间;Spring 的工厂方法;IoC 自动装载(Autowire);AOP以及如何使用; 适用人群:比较适合与我一样的在校普通大学生进行学习整理,以及适合初学spring的朋友进行巩固加深印象! 阅读建议:...

    Spring Security 中文教程.pdf

    2.2. 开始使用安全命名空间配置 2.2.1. 配置web.xml 2.2.2. 最小 &lt;http&gt; 配置 2.2.2.1. auto-config 包含了什么? 2.2.2.2. 表单和基本登录选项 2.2.3. 使用其他认证提供器 2.2.3.1. 添加一个密码编码器...

    Spring API

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring.3.x企业应用开发实战(完整版).part2

    9.5.3 基于tx/aop命名空间的配置 9.6 使用注解配置声明式事务 9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 BEA WebLogic 9.8 小结 第10章 ...

    Spring.net框架

    我现在还不是很了解,而且越学习越发现自己了解的很少,Ioc与AOP中蕴涵了大量的能量等待我们去开发。在这个系列 中,我仅仅利用Sping.net这个框架向大家展示一下Ioc与AOP的强大功能(呵呵,其实写这段话的目的是因为...

    Spring Framewor开发手册

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. ...

    Spring 3 Reference中文

    1.3.1.1 Spring 依赖和基于Spring 13 1.3.1.2 Maven 依赖管理. 14 1.3.1.3 Ivy 依赖管理 15 1.3.2 日志. 16 1.3.2.1 不使用Commons Logging .. 17 1.3.2.2 使用SLF4J 17 1.3.2.3 ...

    spring2.5.chm帮助文档(中文版)

    2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. 对Websphere ...

    Spring攻略(第二版 中文高清版).part1

    第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...

    spring声明式事务管理

    1.掌握Myeclipse的使用。 2.掌握spring框架和hibernate框架的使用。...1.创建web project项目命名为aopweb 2.添加spring支持、hibernate支持 3.配置WEB-INF/applicationContext.xml提供基于AOP的声明式事务

    spring security 参考手册中文版

    12.2.3使用RequestPostProcessor在Spring MVC测试中以用户身份运行 106 作为用户在Spring MVC测试中使用注释运行 108 12.2.4测试HTTP基本认证 109 12.3 SecurityMockMvcRequestBuilders 109 12.3.1测试基于表单的...

Global site tag (gtag.js) - Google Analytics