Spring之环绕通知

一、创建实现接口类:LogAround.java

package org.ruangong.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LogAround implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		 	Object result = null;
		try{
		 		
		 		System.out.println("用环绕通知实现的前置通知");
		 		//invocation.proceed()之前的代码:前置通知
		 		result = invocation.proceed();
		 		//invocation.proceed()之后的代码;后置通知
		 		System.out.println("用环绕通知实现的后置通知");

		 	}catch(Exception e){
		 		System.out.println("用环绕通知实现的异常通知");
		 	}
		return result;
	}

}

  在try{}中,invocation.proceed()之前的代码是前置通知,invacation.proceed()之后的代码是后置通知。

catch中的代码是异常通知:

将业务类和通知类写入ioc容器:

<!-- 环绕通知 -->
	<bean id="logAround" class="org.ruangong.aop.LogAround"></bean>
<aop:config>
		<aop:pointcut expression="execution(public void org.ruangong.service.StudentServiceImpl.addStudent(org.ruangong.entity.Student))" id="pointcut"/>
		<aop:advisor advice-ref="logAround" pointcut-ref="pointcut"/>
	</aop:config>

  测试类中进行测试:

 

异常通知测试:

原文地址:https://www.cnblogs.com/jccjcc/p/13985619.html