码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-04 18:08:29      阅读:463      评论:0      收藏:0      [点我收藏+]

标签:

第一步:配置实现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

 

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

标签:

原文地址:http://www.cnblogs.com/dobestself-994395/p/4272835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!