Spring MVC 注解下Controller 的AOP
在使用spring框架时,通常用它的aop来记录日志,但在spring mvc采用@Controller注解时,对Controller进行Aop拦截不起作用,原因是该注解的Controller已被spring容器内部代理了.需要对org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter进行Aop才能起作用.经过多次测试可行. view plaincopy[*]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代码 view plaincopy
[*]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");
[*] }
[*]}
页:
[1]