spring aop 拦截器 MethodInterceptor 配置

14 篇文章 0 订阅

在此之前呢,这篇文章是基于Spring方法拦截 http://blog.csdn.net/u011229848/article/details/53032369 Spring AOP原理及简单应用 后写的另一种配置方式方法拦截,好了,进入正题:

首先看一下配置文件中aop的配置,其中<aop:pointcut id="serviceMethodPointcut" expression="execution(* com.xqx.fcch.service..*(..))"/>是其切入点,<aop:advisor advice-ref="serviceMethodInterceptor" pointcut-ref="serviceMethodPointcut" />是在该切入点使用自定义拦截器。

<span style="font-family:KaiTi_GB2312;"><span style="white-space:pre">	</span><bean id="serviceMethodInterceptor" class="com.xqx.fcch.exception.ServiceMethodInterceptor"></bean>
    
   	<!-- 方法拦截器(拦截Service包中的所有的方法) MethodInterceptor -->
  	<aop:config>
       	<aop:pointcut id="serviceMethodPointcut" expression="execution(* com.xqx.fcch.service..*(..))"/>
		<aop:advisor advice-ref="serviceMethodInterceptor" pointcut-ref="serviceMethodPointcut" />
	</aop:config></span>

这里需要注意的是,上面的配置文件是对service进行拦截,很多人配置文件拦截请求返回异常,只是对controller方法拦截,这里面我单独用一个aop:config

   	<span style="font-family:KaiTi_GB2312;"><!-- 方法拦截器(拦截controller包中的所有带有@RequestMapping注解的方法) MethodInterceptor -->
	<aop:config>
		<aop:pointcut id="controllerMethodPointcut" expression="execution(* com.xqx.fcch.controller..*(..)) and
        	@annotation(org.springframework.web.bind.annotation.RequestMapping)"/>
		<aop:advisor advice-ref="controllerMethodInterceptor" pointcut-ref="controllerMethodPointcut" />
	</aop:config></span>


注意:一般项目SpringMVC的配置文件扫描Controller,spring的配置扫描Serice,Dao一类的,配置文件单独分开了,所以上面两个不同包的拦截应该 放在不同配置文件中, Controller应该陪在servler的配置文件,也就是MVC下,Service不能放MVC视图,控制的配置文件,应该放在spring的applicationContext中。


下面是拦截器实现java代码:

<span style="font-size:18px;">package com.xqx.fcch.exception;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServiceMethodInterceptor implements MethodInterceptor{
	final Logger log = LoggerFactory.getLogger(ServiceMethodInterceptor.class);
	
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object result = null;  
        StringBuffer info = new StringBuffer();  
        info.append("intercept the method: ");  
        info.append(invocation.getMethod().getDeclaringClass(). getName());  
        info.append(".");  
        info.append(invocation.getMethod().getName());  
        try {  
           result = invocation.proceed();  
        } catch (Exception e) {
        	log.error(info.toString()+"\n"+e.getMessage(),e);
        	throw e;
        }
        return result;  
    }


}</span>


应为我的controller已经有spring全局异常捕获的实现,所以我这里只是对service的拦截记录日志。

以上观点只是个人见解,如有不对的地方还希望大神们多多指教,也希望对读者有所帮助,谢谢。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值