Spring MVC和Spring配置AOP
来源:互联网 发布:blog与mysql的分离 编辑:程序博客网 时间:2024/05/19 19:14
1.首先Spring MVC和Spring是两个东西,配置文件是分开的,配置文件的加载也是分开的。所以要想给Spring MVC的Controller添加AOP的话,得在Spring MVC的配置文件中配置,要想给Service和Repository添加AOP的话得在Spring的配置文件中配置。
2.以一个例子说明,有人配置了Service层的事务AOP之后,事务不起作用。原因是在Spring MVC中配置的扫描规则,将Service也扫描了,这是Spring的配置文件还没有加载,也就还没有对Service层的类添加事务AOP,当加载Spring的配置文件时,再次扫描Service层,发现Service层的类都已经再容器里了,就不往容器里放了,也就是此时加到Spring容器里的Service都是没有添加事务AOP之前的类。解决这个问题的思路很简单,就是在Spring MVC的配置文件中配置,只扫描Controller层的类,不扫描Service层的类,Service层的类在Spring的配置文件中扫描。
3.要实现上述效果,首先看如下的代码
<aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 扫描@Controller组件 --> <!-- 提示:将com.corp.app该为您的业务系统的包路径 --> <context:component-scan base-package="cn.osworks.aos;cn.usmaker.system.plugin;com.mycorp.mysystem" use-default-filters="false"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
use-default-filters=”false”的作用是禁用默认的扫描规则,使用自己配置的扫描规则。也就是在context:component-scan 标签内,context:include-filter 标签里配置的规则,会被扫描,context:exclude-filter 配置的规则不会被扫描。
上边的配置是Spring MVC的配置文件中的配置,只扫描带有org.aspectj.lang.annotation.Aspect和org.springframework.stereotype.Controller、org.springframework.web.bind.annotation.ControllerAdvice的标记。
Spring的配置如下:
<aop:aspectj-autoproxy/> <!-- Spring服务组件扫描(排除@Controller相关组件) --> <!-- 请将com.mycorp.mysystem.**改为你的项目扫描路径 --> <context:component-scan base-package="cn.osworks.aos;cn.usmaker.system.plugin;com.mycorp.mysystem" use-default-filters="false"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
只扫描org.aspectj.lang.annotation.Aspect和org.springframework.stereotype.Service标记的类。
4.注意Spring MVC中的配置和Spring的配置的一点区别
在Spring MVC的配置是
<aop:aspectj-autoproxy proxy-target-class="true"/>
在Spring中的配置是
<aop:aspectj-autoproxy/>
5.配置的切点尽量精准,这点很重要,要代理哪些类就精确的配置到哪些类,不然很容易代理一些Spring和Spring MVC内部使用的类,容易产生错误,甚至启动不起来。
- Spring MVC和Spring配置AOP
- Spring MVC 配置 AOP
- spring mvc中配置aop
- Spring mvc 对Controller配置AOP
- aop切面配置不起作用引发的spring容器和spring mvc容器关系学习
- Spring AOP 和 MVC 拦截器
- Spring MVC AOP编程
- Spring mvc aop 无效
- Spring 和 Spring MVC 整合 AOP 不起作用的解决方案
- Spring MVC 配置和实例
- spring mvc aop配置事务的相关配置
- spring mvc aop配置事务的相关配置
- 【spring mvc】spring-mvc配置
- spring aop 到mvc 的controller配置不起作用
- 【Spring】Spring基础配置-AOP
- spring之aop术语和配置
- spring之AOP基本概念和配置详解
- 2012年终总结spring mvc-----AOP
- 【网络流】:一些基本知识
- Android 之自定义弹出提示框
- Hive内存溢出的问题
- Struts2中的OGNL详解
- 关于iOS中如何给字体加粗的问题总结
- Spring MVC和Spring配置AOP
- android 禁止scrollview 因控件变化自动滚动到底的方法
- iOS使用自定义字体
- 偏光片在LCD中的应用
- getParameter 与 getAttribute的区别
- C++开发人脸性别识别教程(8)——搭建MFC框架之读取文件夹信息
- 搜狗微信公众号文章抓取
- 20点提高网站访问速度缩短网页加载时间
- vim 中如何进行列编辑。