您的位置:首页 > 编程语言 > Java开发

Spring Xml文件配置实现AOP通知

2014-04-26 01:45 357 查看
在前一篇文章,我们讲述了注解实现AOP的通知,这一片,我们看一下,xml文件是如何配置AOP通知的

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

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://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>


运行效果如下:

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

文章源代码下载:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: