转自http://usherlight.iteye.com/blog/1306111

正确配置spring aop,在controller中使用AOP

在controller中使用AOP的问题主要在于如何让controller能够被检测到。 
controller和其他spring bean的区别在于:controller是由mvc定义并在web.xml中的dispatcher中定义的。

解决方法: 
1、正确定义controller,(比较通用的做法,没有特殊情况的话,大部分应用没有这个问题) 
    a. 将服务层的类都放在ApplicationCotext-*.xml中定义,在context listener中初始化(注意,任何controller都不应该在这里出现),要包括<aop:aspectj-autoproxy/>, 在这里,有没有proxy-target-class="true" 没有关系(具体含义参看下文) 
    b. 定义mvc的配置文件,一般是 <<servlet name>>-servlet.xml,一般(也是推荐做法)使用auto scan来定义所有的controller.关键步骤来了:这个文件也要加入<aop:aspectj-autoproxy proxy-target-class="true"/>, 一定要添加proxy-target-class="true"! 这是用于通知spring使用cglib而不是jdk的来生成代理方法。 
    c. 另外一个事项,controller需要使用@controller注释,而不是继承abstract controller。 
    d. 建议使用aspectj来完成aop

SpringMVC中对Controller使用AOP的更多相关文章

  1. SpringMVC中的Controller默认单例

    众所周知,Servlet是单例的. 在struts中,Action是多例的,每一个请求都会new出来一个action来处理. 在Spring中,Controller默认是单例的,多个请求都会访问同一个 ...

  2. springmvc中的controller是单例的

    今天发现spring3中的controller默认是单例的,若是某个controller中有一个私有的变量a,所有请求到同一个controller时,使用的a变量是共用的,即若是某个请求中修改了这个变 ...

  3. springMVC中的Controller里面定义全局变量

    转自:http://notebookdong.iteye.com/blog/1869852 使用SpringMVC的时候,如果想要在Controller中定义一个全局变量,并且实现在不同用户访问程序的 ...

  4. SpringMVC中的@Controller和@RequestMapping到底什么鬼?

    1.1 @Controller是什么 首先看个例子: @Controller @RequestMapping("/blog") public class BlogControlle ...

  5. SpringMVC中在Controller类的每个方法执行前调用某个方法的实现

    在使用SpringMVC做项目的时候,如果想在@Controller类中每个@RequestMapping方法执行前都调用某个方法,要怎么实现呢?答案是使用Spring的@ModelAttribute ...

  6. springMVC中一个controller多个方法

    web.xml文件: <?xml version="1.0" encoding="UTF-8"?><web-app version=" ...

  7. SpringMVC中的controller默认是单例的原因

    http://lavasoft.blog.51cto.com/62575/1394669/ 1.性能 :单例不用每次new浪费资源时间. 2.不需要:一般controller中不会定义属性这样单例就不 ...

  8. 方法调用---springMVC中调用controller的方法

    我们有一个路由StudentController,里面有一个方法count().如果要在另外一个GradeController中调用count()方法有2种方式: 因为StudentControlle ...

  9. Spring|SpringMVC中的注解

    文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...

随机推荐

  1. discuz管理中心无法登陆

    检查下配置文件,当前管理是不是创始人. 如是,那试下修改数据库,在数据表出错时也会这样,还有一个也同时试下 \config\config_global.php 文件 $_config['admincp ...

  2. grails的插件

    今天来歪理邪说一下grails的插件. 有个问题让本人困惑了一段时间,插件是属于grails的,还是属于某个工程的?为什么会有这个问题呢,这涉及到grails插件的安装方式. grails的插件像是一 ...

  3. jquery返回顶部特效

    <style> p#back-to-top{position:fixed; bottom:100px;right:10px; display: none; } p#back-to-top ...

  4. 《C和指针》 读书笔记 -- 第9章 字符串、字符和字节

    1.字符串以NUL结尾,但字符串长度不包括NUl字节. 2.复制字符串 char *strcpy(char *dst,char const *src); 3.连接字符串 char *strcat(ch ...

  5. Ajax的理解

    初学JS,一直认为Ajax是个很高级的.不可亵玩的东西.这两天怀着忐忑的心情接触了一下它, 感觉它并没有想象中的那么难理解. 其实,Ajax就是浏览器端向服务器请求资源的一个对象(方法). 就跟打电话 ...

  6. 1062 Talent and Virtue (25)

    /* L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of ta ...

  7. redis参考

    www.redis.cn www.redis.io http://blog.nosqlfan.com/ 可以移步http://try.redis.io/进行实验命令 Redis 设计与实现(第一版) ...

  8. 微软职位内部推荐-Senior NLP Scientist & Developer

    微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com )Senior Software Development Engineer/NL ...

  9. 运行windows系统工具命令

    appwiz.cpl 卸载/安装程序  wscui.cpl 操作中心 inetcpl.cpl  查看Internet属性  eventvwr     查看监视消息和疑难解答消息  taskmgr  任 ...

  10. iOS应用中URL地址如何重定向-b

    就用一个很简单的例子 http://www.google.com谷歌的首页 都知道现在浏览器中打开google.com的话事实上会变成http://www.google.com.hk 网址被重定向了 ...