关注JEECG发展历程 关注最新动态和版本, 记录JEECG成长点滴 更新日志 - 技术支持 - 招聘英才

JEECG最新版本下载 JEECG智能开发平台 - 显著提高开发效率 常见问题 - 入门视频 - 参与开源团队

商务QQ: 69893005、3102411850 商务热线(5*8小时): 010-64808099 官方邮箱: jeecgos@163.com

查看: 9751|回复: 0

Spring MVC 注解下Controller 的AOP

[复制链接]
发表于 2013-9-5 21:37:16 | 显示全部楼层 |阅读模式

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


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表