首页
学习
活动
专区
圈层
工具
发布

AOP切面编程二 原

一、前置通知递参数

1.修改前置通知接收参数选项

代码语言:javascript
复制
public void beforeInvoke(Object arg){
     Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Before] 参数值 "+arg); 
    }

2.修改applicationContext 调用表达式和前置通知参数接收

代码语言:javascript
复制
 <!-- 针对AOP的处理进行配置 -->
<aop:config>
     <!-- 定义业务操作切入点 -->
   <aop:pointcut expression="execution(* group.esperanto.service..*.*(..)) and args(id)" id="messagePoint"/>
        <!-- 定义具体操作到切入点的方法上 -->
   <aop:aspect ref="serviceProxy">
       <aop:before method="beforeInvoke" pointcut-ref="messagePoint" arg-names="id"/>
       <aop:after method="afterInvoke" pointcut="execution(* group.esperanto.service..*.*(..))"/>
   </aop:aspect>
</aop:config>

二、返回通知

1.添加返回通知处理方法

代码语言:javascript
复制
public void returnInvoke(Object val){
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Returning] 返回值 "+val); 
     }

2.修改applicationContext 添加返回通知

代码语言:javascript
复制
<!-- 针对AOP的处理进行配置 -->
<aop:config>
        <!-- 定义业务操作切入点 -->
  <aop:pointcut expression="execution(* group.esperanto.service..*.*(..)) and args(id)" id="messagePoint"/>
        <!-- 定义具体操作到切入点的方法上 -->
  <aop:aspect ref="serviceProxy">
    <aop:before method="beforeInvoke" pointcut-ref="messagePoint" arg-names="id"/>
    <aop:after method="afterInvoke" pointcut="execution(* group.esperanto.service..*.*(..))"/>
    <aop:after-returning method="returnInvoke" pointcut="execution(* group.esperanto.service..*.*(..))" returning="val" arg-names="val" />
  </aop:aspect>
</aop:config>

三、环绕通知

1.添加环绕通知执行方法

代码语言:javascript
复制
public Object arroundInvoke(ProceedingJoinPoint point) throws Throwable{
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy -arroundInvoke Before] 参数 值 "+ Arrays.toString(point.getArgs())); 
           // Object obj = point.proceed(point.getArgs());  // 正常继续传递参数
    	 Object obj = point.proceed(new Object[]{"hello"});   //现在不传递参数,自定义参数传递
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy -arroundInvoke After] 返回结果"+obj); 
    	 return true;
     }

2.修改applicationContext

代码语言:javascript
复制
 <!-- 针对AOP的处理进行配置 -->
<aop:config>
        <!-- 定义业务操作切入点 -->
  <aop:pointcut expression="execution(* group.esperanto.service..*.*(..))" id="messagePoint"/>
     <!-- 定义具体操作到切入点的方法上 -->
  <aop:aspect ref="serviceProxy">
     <aop:around method="arroundInvoke" pointcut-ref="messagePoint"/>
  </aop:aspect>
</aop:config>
下一篇
举报
领券