zoukankan      html  css  js  c++  java
  • 14Spring_AOP编程(AspectJ)_环绕通知

    在目标方法执行前后,进行代码增强 (阻止目标方法的执行 )

    环绕通知实现任何通知效果。

      案例如下:

    案例结构:

    第一步:编写代理类。

    MyAspect.java

              

    	//这里必须要抛异常
    	public Object around( ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    	{
    		System.out.print("环绕通知 方法前执行");
    		Object result=proceedingJoinPoint.proceed();
    		System.out.print("环绕通知 方法后执行");
    		return result;
    		
    		
    		
    		
    	}
    

     第二步:写ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               http://www.springframework.org/schema/beans/spring-beans-2.5.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
                               http://www.springframework.org/schema/tx 
                               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
         <bean id="OrderService" class="cn.itcast.spring.c_aop.impl.OrderServiceimpl" />
    
    
    
    
    
    
    
    
    
    
    <!-- AspectJ AOP -->
    <!-- 配置目标 -->
    <bean id="CustomerService" class="cn.itcast.spring.d_aspectj.CustomerService"></bean>
     <!-- 配置切面类 -->
    <bean id="MyAspect" class="cn.itcast.spring.d_aspectj.MyAspect"></bean>
     <aop:config>
     <!-- ref引用切面类 -->
     <aop:aspect ref="MyAspect">
     
         <aop:pointcut expression="execution(* cn.itcast.spring.d_aspectj.CustomerService.*(..))" id="mypointcut2"/>
    
        <aop:around method="around" pointcut-ref="mypointcut2"/>
     
     
     </aop:aspect>
    
    </aop:config>
    
    
    
    
    </beans>
    

     第三步 写Junit测试的代码:

    	@Test
    	public void testaround()
    	{
    		
    		customerService.delete();
    		
    	}
    

      第四步  输出结果:

    环绕通知 方法前执行
    this is delete
    环绕通知 方法后执行
    
  • 相关阅读:
    UVA1401 Remember the word DP+Trie
    LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化
    模拟赛总结合集
    LG5201 「USACO2019JAN」Shortcut 最短路树
    LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
    LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理
    20190922 「HZOJ NOIP2019 Round #7」20190922模拟
    LG2530 「SHOI2001」化工厂装箱员 高维DP+记忆化搜索
    LG2893/POJ3666 「USACO2008FEB」Making the Grade 线性DP+决策集优化
    关于对QQ 输入法的评价
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/5717962.html
Copyright © 2011-2022 走看看