后置通知和前置通知差不多。最大的特点是因为后置通知是运行在目标方法之后的,所以他可以拿到目标方法的运行的结果。
给出案例:
案例结构图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARYAAAB9CAIAAADoa9s1AAASe0lEQVR4nO2dW2wU1xnHTx6qvlTqQyrlMWqrJiGoVW1s2k4pTa9BbVWqpgGcxpQ00igJigptc+FiiM3iWISk0yYpuQAxuYBr3JYGMgRRQpFKMbk4hSYNYmSMiQF77TWLr7te0+nDzM6c68ysZ8dzxv5++h52zn7nMrvnv+fM7JzvIHPauXii+V+bv4TbxRPN098MACgLKO4GAECyAQkBQChAQgAQCpAQAIQCJAQAoQAJAUAoJJLQ6gU3xFm9oSlI0Yw4myCkvG3TVVlOVObPPDBxSuhoQ8WxtV907Ok7b8QPjzZUTGtrZP46QUISE6eEjj0y1zi68+LJXRdP7urY+8TOVcvww2OPzI2pXYamIFWXoZBoiFlCEn8yUyIqCR09erSpqcnH56FbL57clb96OHNq90dvbtu5ahl+ePShWyNqmx8goUiR+JOZEpFI6MiRI3Pnzr3pppu83Q6vvvXiyV2ZU7szp3Z/dOg5S0LO4eHVYgmlW1rq7n837dsQXUU2imbYfUdTsRQM+5t1sxQ9qEIsTxunK5BpbCFcN6dW179YIJauaIbb60Sn4JSgaBpXIViTOCcvbF8pbeZ9VnSiRnwyM0JN5ZeQpZ9ly5b5Sujgg7d07H3ioze3fXTouRPNDWsW34YfHnzwFs/cH7/7zO3P1t3+bN2WTr6DrtJdRVeJbsD2bryLiAoxNLV4rKt2cZyuwOsdAjdE1Oa2gRaU8BTcbHZGWiG4A+eUpnBqvDbjo5uTjfcBOiWChFgc/dTU1HhLaPWCG56+88adq5ZZtmbxbWsW3zaVKtMtLXW3H/qQSedMV6jvOICEBHMe5ofZ+qHFXbm9w9fNOSTTqVGIOgWqkWybfR1CnVrxkBzn7DycukBCYnD9+ErINM2/3PsF/P6BJaHrN32IrnsSXffk9ZtYWeD4jUIRSQj/lad7gNX9rDc9eo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不多,这里每个类就不做具体解释了。
第一步:
编写目标类:
package cn.itcast.spring.d_aspectj; //目标业务类
public class CustomerService { public int delete() {
System.out.println("this is delete");
return 1;
} }
第二步:
编写切面类
//切面类(内部可以写很多的Advice的方法)
public class MyAspect { //可以传两个参数,后置通知相比前置通知来时
//一个特点是,因为后置通知是运行在目标方法之后的,所以他可以拿到目标方法的运行的结果
public void afterReturning(JoinPoint joinPoint,Object result)
{
System.out.print("后置通知的运行"+result); } }
第三步:编写Spring配置文件
<!-- 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"/> <!-- 这个参数是配置返回的 必须和public void afterReturning(JoinPoint joinPoint,Object result)这个方法里面的 result一字不差-->
<aop:after-returning method="afterReturning" returning="result" pointcut-ref="mypointcut2" />
</aop:aspect>
第三步:编写测试类
@Test
public void testafter()
{System.out.print(customerService.getClass().getName());
customerService.delete(); }
输出结果:
this is delete
后置通知的运行1
看到没 !返回了目标函数的执行结果。