Spring Xml文件配置实现AOP通知

来源:互联网 发布:手机淘宝加不了购物车 编辑:程序博客网 时间:2024/05/29 03:45

在前一篇文章,我们讲述了注解实现AOP的通知,这一片,我们看一下,xml文件是如何配置AOP通知的

项目结构跟上一篇文章的项目结构一样,只不过在StudentsLog.java中没有使用aop通知的注解,而是在applicationContext.xml中配置的

application.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config/><context:component-scan base-package="com.spr"/><aop:aspectj-autoproxy/><!-- 定义切面类 --><bean id="StudentsLog" class="com.spr.log.StudentsLog"/><!-- 定义AOP(切点、切面、通知) --><aop:config><!-- 配置切入点 --><aop:pointcut expression="execution(public void com.spr.studentsDAOImpl.StudentsDAOImpl.*Students(*,*)) and args(sid,sname)"   id="saveStudentsPointCut"/><aop:pointcut expression="execution(public * com.spr.studentsDAOImpl.StudentsDAOImpl.*Students(*)) and args(sid)"   id="queryStudentsPointCut"/><!-- 配置切面 --><aop:aspect id="saveStudentsAspect" ref="StudentsLog"><!-- 配置before通知 --><aop:before method="saveBefore" pointcut-ref="saveStudentsPointCut" arg-names="sid,sname"/><!-- 配置after通知 --><aop:after method="saveAfter" pointcut-ref="saveStudentsPointCut" arg-names="sid,sname"/><!-- 配置afterThrowing通知 --><aop:after-throwing method="saveAfterThrowing" pointcut-ref="saveStudentsPointCut" arg-names="sid,sname,ex" throwing="ex"/><!-- 配置环绕通知 --><aop:around method="saveAround" pointcut-ref="saveStudentsPointCut" arg-names="sid,sname"/></aop:aspect><aop:aspect id="queryStudentsAspect" ref="StudentsLog"><!-- 配置afterReturning通知 --><aop:after-returning method="queryAfterReturning" pointcut-ref="queryStudentsPointCut" arg-names="sid,students" returning="students"/></aop:aspect></aop:config></beans>

运行效果如下:


有关于通知的介绍,请看上一篇博文

文章源代码下载:点击打开链接

0 0
原创粉丝点击