Spring Xml文件配置实现AOP通知

在前一篇文章,我们讲述了注解实现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>

运行效果如下:


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值