advice的五种类型

通知的5种类型:执行顺序:相同类型已配置顺序为准
1.before:<aop:before method="before" pointcut-ref="pt"/>
2.after:<aop:after method="after" pointcut-ref="pt"/> 
3.afterReturning:返回后加入,有异常不执行<aop:after-returning method="afterReturning" pointcut-ref="pt"/>
4.afterThrowing:有异常才会执行<aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>
5.round:<aop:around method="round" pointcut-ref="pt"/>
用法最广泛,可替代before,after,调用目标方法
public void round(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("round before");
pjp.proceed();
System.out.println("round after");

}


代码实现:

    1.目标对象:

    



	public void save() {
		//int i = 1/0;
		System.out.println("save.....");
	}
}

    2.通知:

    

public class MyTarget2 {
public class MyAdvice2 {
	public void before() {
		System.out.println("before");
	}
	public void after() {
		System.out.println("after");
	}
	public void afterReturning() {
		System.out.println("afterReturning");
	}
	public void afterThrowing() {
		System.out.println("afterThrowing");
	}
	public void round(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("round before");
		pjp.proceed();
		System.out.println("round after");
	}
}

    3.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:util="http://www.springframework.org/schema/util"
       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.xsd
	   http://www.springframework.org/schema/util
	   http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd">
		
	<!--spring管理资源都是bean的形式  -->
	<!-- 你的资源是那个类的对象 
		<bean class="类名" id="类名对应的唯一标识"></bean>
	-->
	<!--配置aop,切入点与通知之间的关系   _切面 -->
	<!--aop:config:配置aop  -->
	<aop:config>
		<aop:pointcut expression="execution(void com.array.aop.adviceType.MyTarget2.save())" id="pt"/>
		<aop:aspect ref="myAdvice2">
			<!--配置aop的通知类别  
				aop:before method="" 通知类别的具体通知
				aop:before pointcut=""切入点
				<aop:before method="fn1" pointcut="执行到save方法时"/>
				
		 -->
		 	<!-- 在方法save的前面加入方法fn -->
			<!-- <aop:before method="before" pointcut-ref="pt"/>
			<aop:after method="after" pointcut-ref="pt"/> -->
			<!-- <aop:after-returning method="afterReturning" pointcut-ref="pt"/> -->
			<aop:around method="round" pointcut-ref="pt"/>
			<aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>
		</aop:aspect>
	</aop:config>
	<!--/将目标对象与通知定义为bean  -->
	<bean id="myAdvice2" class="com.array.aop.adviceType.MyAdvice2"></bean>
	<bean id="myTarget2" class="com.array.aop.adviceType.MyTarget2"></bean>
</beans>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值