在目标方法执行前后,进行代码增强 (阻止目标方法的执行 )
环绕通知实现任何通知效果。
案例如下:
案例结构:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASsAAAB1CAIAAACZNVDQAAAR00lEQVR4nO2dW4wb1RnHh4eKl0p9oBKPqFQFQlSpCbFZ3BAopSUqVVMVgRIJNptEsgIRIil3SEp3sxdnE+i0FBIEhIRLcJItpFA5RDSNeEhJopBV0iAkW7shuwvZ9a4Xry9rj73S9GFu5zoz3vV4ztjfTyNl58x3LnbO3+fMsc9/JFUATnQt+ey5n5rHy/dfh56e6FrS0Nak5IgUkVMNrdMt9W1bIirKCxX5PfcYye8GqKqqfvb04tSJvd+c3v/N6f3nDu/cu3k1evrZ04t9aldKjkjRhAiFeIPPChT4nWkgnivwxIkTsVjMIebJm785vV+Z+TRz/sBXn+zeu3k1enriyZu9biQHUKCnCPzONBBvFXj8+PHFixffcMMN9mGfbrn5m9P7M+cPZM4f+OrYHk2B5umnW/gKTMfj2x4+m3ZsSCIq6UTklN715CiSgqB3DCuLEUEUokXqmD0JT6MLYYaZtVrxRoFIekROWZ2W9xLMEiKyzBQY0iTGi+e2r5Y2s94rMlHG3pkWFqOHCtTkt3r1akcFHn30pnOHd371ye6vju35fF/Xs6vuRE+PPnqTbe7Rs3+/55Vt97yyrX+IHZCIkj0tEcV6ES0OtIfxCknJUeM8EdWLY/QkVufihElYbVYbSD1yX4KVTc9ICgwNYLykebw0VpvRsdXMxnoDzRJBgXXHlN+aNWvsFbhl+bUv33/d3s2rtePZVXc+u+rO+VSZjse33XPsSyqdMdciuogLBXImbNSwoH3Mo6HMzuUYZp7i6cQYSLwEopF0mx0DFvTSjFN8lNXzMOoCBaqqRwpE5eeoQFVVP9jwE3TpRVPgNdu/lK56UbrqxWu206pCcRoDPVIgOsaQHUjrvdpFm85lE+arAuf50hAF0sWCAjl4osBYLHYDQm9vr338wY4fEysxqqqa2pOuepGb09V9IDr1SshkVzW6rzmX489CsUKQMlJyxJyqyeR8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sxVsHoPSRRkZHqfiWGUiA3vJZCrGZgLaU/EqxPD7zl+t0fFQYKbARCKLDrmd9eTpcvT5ZGJssjk+WRtG42YTcF5cKbYtVrCR4A6okQCvzTU7/7eqJ0aaL0tXEsoDBQIBAkhFDg80+sIg4tHbV7cQ0oEAgSQigQAFoWUCAA+AkoEAD8BBQIAH4CCgQAPwEFAoCfgAIBwE/8V6AkSd+7+mr7Q5KY7azrr6KE9XoRtmFAPRBCgT94OG5/eKVActODkB1d2IYB9aCVFZiSI5GIoL0bfsHTKgihQDewsi5IgdrmAjunCD8BBbYKQihwvjGoAtmbfJD0iCzb7lNn9HkXljOUOwu5H4qRHYtHNx4aZUbMHUY2W5+w/baMFw4EA1EUaA50zNHPSYF2xirYnj9KbY4KdLac4dZu40xDb1cnyuRt/6UywmAZcERR4LwwFMgzViHSjVO2txF3DKRP8XSurQsnO9vNhVcXboHByEhYSwABQxQFEmOg6sYnxk6BLCsk/RSZtaEzx3oqkPaPULmfCw51OVtImUGgwyAiigIZ6Y4+MTazUOT2jDULxYogx0Br9d/Ocoa4qWPVbuNMQ7u5uFAgM2NKlhNUFiA4iKJAegzUr9r5xLhYiUEcXrCVGKQIewVyLWcSRDF07bzszMUTsky2CQ1r1YW51AMEBVEUyL40H58YPvbfPDjPQmsFBiXAGSEUSPwAbQFrMwQpOcJZsWSFUpdBgYDn+K9AG+blE4ODTNu48tOncbRYQIGA5witQABoekCBAOAnoEAA8BNQIAD4CSgQAPzk/1mhXh80lFn1AAAAAElFTkSuQmCC" alt="" />
第一步:编写代理类。
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
环绕通知 方法后执行