zoukankan      html  css  js  c++  java
  • 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>
    

      测试类中进行测试:

     

    异常通知测试:

  • 相关阅读:
    [洛谷P2184]贪婪大陆
    [BJOI2006]狼抓兔子
    [JSOI2007]重要的城市(x)
    [NOIP2011提高组]Mayan游戏
    gitee 使用
    部分激光打印机清零方法
    django2.0内置分页
    django上下文处理器
    jquery键盘事件
    类视图装饰器
  • 原文地址:https://www.cnblogs.com/jccjcc/p/13985619.html
Copyright © 2011-2022 走看看