• AOP编程实例:拦截器拦截拦截某个实现类的方法


    第一步:配置实现MethodInterceptor的切面

    java代码

     1 public class OutsideInvokeLogInterceptor implements MethodInterceptor{  
     2     private static Logger log = Logger.getLogger("outsideInvoke");  
     3     
     4     @Override  
     5     public Object invoke(MethodInvocation invocation) throws Throwable {  
     6         String methodName = invocation.getMethod().toString();  
     7         Object returnValue = invocation.proceed();  
     8         if (returnValue == null) {  
     9             log.warn("调用 " + methodName + "is fail,返回的result结果为空,方法入参为:(" + parseArguments(invocation.getArguments()) + ")");  
    10             return ResultDTO.getFailureResult(BSErrorCode.INTERNAL_ERROR, BaseKeyMsgCode.RETURN_RESULTDTO_IS_NULL);  
    11         }else{  
    12             if(returnValue instanceof ResultDTO){  
    13                 ResultDTO<Object> resultDTO = (ResultDTO<Object>)returnValue;  
    14                 if(!resultDTO.isSuccess()){  
    15                     log.warn("调用 " + methodName + " is fail ,方法入参为:(" + parseArguments(invocation.getArguments()) + ")"  
    16                          + "返回值:" +resultDTO.getCode() + "," + resultDTO.getKey());  
    17                 } else{  
    18                     log.info("调用 " + methodName + " is success ,方法入参为:(" + parseArguments(invocation.getArguments()) + ")");  
    19                 }  
    20             }  
    21         }  
    22         return returnValue;  
    23   
    24     }  
    25 }  
    View Code
    1 <bean id="outsideInvokeLogAdvice" class="com.ali.luna.commons.service.interceptor.OutsideInvokeLogInterceptor" /> 
    View Code

    配置切点:

    1 <bean id="methodPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">  
    2         <property name="mappedNames">  
    3             <list>  
    4                 <value>add*</value>  
    5             </list>  
    6         </property>  
    7     </bean>  
    View Code

    配置自动代理:

     1 <bean id="outsideInvokeLogInterceptor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
     2     <property name="pointcut" ref="methodPointcut"/>  
     3     <property name="advice" ref="outsideInvokeLogAdvice"/>  
     4 </bean>  
     5 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
     6     <property name="beanNames">  
     7         <list><value>activityService</value></list>  
     8     </property>  
     9     <property name="interceptorNames">  
    10         <list>  
    11             <value>outsideInvokeLogInterceptor</value>  
    12         </list>  
    13     </property>  
    14 </bean>  
    View Code
  • 相关阅读:
    WPF学员管理系统
    dotnet-千星项目OpenAuthNet基于NetCore21的快速开发框架
    MVC5仓库管理系统
    华为设备IPC V200R002C0SPC SDK连接demo
    基础界面
    交通建设项目管理信息化系统
    Apache常见interview
    mamcached+(magent+keepalived高可用)搭建及理论概述
    TCP/IP三次挥手,四次断开(精简)
    简述FTP的主动模式与被动模式(精简)
  • 原文地址:https://www.cnblogs.com/dobestself-994395/p/4272835.html
Copyright © 2020-2023  润新知