您的位置:首页 > 编程语言 > Java开发

Spring MVC 注解下Controller 的AOP

2010-10-07 14:54 204 查看
在使用spring框架时,通常用它的aop来记录日志,但在spring mvc采用@Controller注解时,对Controller进行Aop拦截不起作用,原因是该注解的Controller已被spring容器内部代理了.

需要对org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter进行Aop才能起作用.经过多次测试可行.

package com.autoabacus.dal.controller;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Aop {
public Aop() {
System.out.println("Aop");
}
// @Around("within(org.springframework.web.bind.annotation.support.HandlerMethodInvoker..*)")
@Around("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))")
public Object aa(ProceedingJoinPoint pjp)  throws Throwable
{
try {
Object retVal = pjp.proceed();
System.out.println(retVal);
return retVal;
} catch (Exception e) {
System.out.println("异常");
return null;
}
}
}


Controller代码

package com.autoabacus.dal.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WelcomeController {
@RequestMapping
public void welcome() {
if (true)
throw new RuntimeException("fdsafds");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: