springmvc 中配置aop
之前自己搭建了springmvc+spring+mybaits/hibernate 的框架,并在applicationcontext.xml中配置了aop,但 发现aop根本不生效,而不用框架的话则可以生效。
applicationContext.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="springframe"></context:component-scan>
<context:property-placeholder location="classpath:/configFile/c3p0.properties" ignore-unresolvable="true"/> <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="${dataSource.driverClassName}"></property>
<property name="jdbcUrl" value="${dataSource.url}"></property>
<property name="user" value="${dataSource.username}"></property>
<property name="password" value="${dataSource.password}"></property> </bean> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*_mapper.xml"/>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="springframe.dao" />
</bean> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect ref="aopBean" >
<aop:pointcut expression="execution(* springframe.service.EmployeeService.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config> <!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"/>
</bean> </beans>
springmvc的配置文件,spring-servlet.xml的配置如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**"/>
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="springframe" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" /> </beans>
观察发现,在application.xml和spring-servlet.xml中都配置了
<context:component-scan base-package="springframe" />
这样的话,aop就不生效了
问题: aop是配置在application.xml中的,容器启动的时候开始扫描,此时本来aop是可以生效的,但在spring-servlet.xml中又再次扫描了一次指定包下的类,但spring-servlet.xml只负责controller,其它的一概不管,这样就导致了aop与service关联的实效,从而导致,aop不生效。
解决办法:spring容器正常扫描包,不过一般不让它扫描controller,因为springmvc还要再次扫描controller;springmvc配置只让它扫描controller,这样就避免了aop类与service失去关联,避免aop不生效。
----------初次发表博客,请多关照。
springmvc 中配置aop的更多相关文章
- SpringMVC中配置AOP拦截controller 失效
来源:http://www.oschina.net/question/222929_124314 目测大部分同学的aop失效都是因为在springmvc的配置文件里面扫描了类,那么spring去扫描的 ...
- Springmvc中配置Quartz使用,实现任务实时调度。
菜鸡的自我修炼,第一次接触quartz,做个记录.-------jstarseven 最近在项目中,第一次在springmvc中配置实用quartz,深刻的感受到quartz带来的方便,顺手做个记录. ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- Spring实战(十一) 在Spring XML中配置AOP
如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...
- SpringMVC 中配置 Swagger 插件.
一.简介 Swagger的目标是为REST API定义一个与语言无关的标准接口,允许用户发现和理解计算机服务的功能,而无需访问源代码.当通过Swagger正确定义时,用户可以用最少量的实现逻辑理解远程 ...
- 在springmvc中配置jedis:
主要学习https://github.com/thinkgem/jeesite.一下代码均参考于此并稍作修改. 1.jedis 首先,需要添加jedis: <!--jedis--> < ...
- 在springmvc中配置jedis(转)
主要学习https://github.com/thinkgem/jeesite.一下代码均参考于此并稍作修改. 1.jedis 首先,需要添加jedis: <!--jedis--> < ...
- springmvc中配置servlet初始化类
<bean id="InitStart" lazy-init="false" init-method="InitSystem" cl ...
- springmvc中配置RESTful风格控制器
一般的http请求中其实只需要get和post就可以满足项目需求了,而为什么还要使用restful可能就是为了使请求url看起来更加直观,好看吧.. restful常用的请求方式:get,post,p ...
随机推荐
- 操作符重载(day07)
二十 操作符重载 eg:复数x+yi +4i (+2i) + (+4i) = +6i 双目操作符(L # R) 1.1 运算类的双目操作符:+ - * / -->左右操作数可以是左值也可以是右值 ...
- Linux—Ubuntu14.0.5安装MySQL
1.更新资援列表 sudo apt-get update 2.安装mysql的操作命令(下一步选中“Y”) sudo apt-get install mysql-server 3.输入MySQLroo ...
- 搭建rsync服务(端口号873)
rsync详细参数 1.-v,--verbose 详细模式输出,传输是的进度信息 2.-z,--compress 传输是进行压缩以提高传输效率,--comperess -level = NUM可以按级 ...
- Java基础之 HelloWorld
1. Java发展史 参考: https://www.cnblogs.com/guoqingyan/p/5667064.html 2. Java中 JDK, JRE, JVM之间的关系 参考: htt ...
- NumPy常见的元素操作函数
ceil(): 向上最接近的整数,参数是 number 或 array floor(): 向下最接近的整数,参数是 number 或 array rint(): 四舍五入,参数是 number 或 a ...
- sql语句学习(NOT EXISTS 和 NOT IN )
NOT EXISTS SELECT a.*FROM t_user aWHERE a.id_card LIKE '%3203821995100%'AND NOT EXISTS ( SELECT id F ...
- 2.1 SVN的安装
一.SVN客户端安装 运行TortoiseSVN-1.6.6.17493-win32-svn-1.6.6.msi程序, 开始安装 点击Next, 下一步 选择 I accept 接受, 点击Next ...
- 《你又怎么了我错了行了吧》第八次团队作业:Alpha冲刺
项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十二 团队作业8 团队名称 你又怎么了我错了行了吧 作业学习目标 (1)掌握软件测试基础技术 (2)学习迭代式增量软件开发过程,完 ...
- 0809MySQL-InnoDB Compact 行记录格式
InnoDB存储引擎提供了compact(5.1后的默认格式)和redundant两个格式来存放行记录数据.redundant格式是为了兼容之前的版本而保留. mysql> show table ...
- 0320SQL中的where条件,在数据库中提取与应用浅析
转自 何登成的技术博客 追求技术的道路上,10年如一日 首页 关于我 RSS 订阅 © 2012-2017 何登成的技术博客 SQL中的where条件,在数据库中提取与应用浅析 3月 3r ...