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才能起作用.经过多次测试可行.
Controller代码
需要对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"); } }
相关文章推荐
- Spring MVC AOP通过注解方式拦截Controller等实现日志管理
- Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理
- Spring MVC 注解下Controller 的AOP
- Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理
- Spring MVC AOP通过注解方式拦截Controller等实现日志管理
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring MVC 中的基于注解的 Controller
- 使用Spring MVC的@ControllerAdvice注解做Json的异常处理
- Spring MVC基于注解的Controller
- Spring MVC 中的基于注解的 Controller
- Spring MVC注解方式service和controller的扫描顺序
- Spring MVC 中的基于注解的 Controller
- Spring MVC 中的基于注解的 Controller
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring MVC中基于注解的 Controller
- 基于Spring MVC中@ControllerAdvice注解实现全局异常拦截