到今天已经使用Mybatis完成了公司交给的demo程序的增删改查,以后有时间把自己的代码结果发上来供大家拍砖,这次要说的是,需求中需要在每个方法执行的时候打印一下系统执行的时间,我使用类似事务管理的aop方式在xml中配置如下。
发现当调用controller方法的时候根本不会进入,但是我把serviceImpl的advice-ref改成methodAdvice的时候发现能够进入执行对应的方法。然后从网上查了一下发现。
主要有两个解决方法:第一个是添加下载aspectj-1.6.10.jar和aspectjweaver-1.6.9 事实证明只对了一部分 第二个方法是修改servlet.xml去掉 <mvc:annotation-driven/>然后添加 <aop:aspectj-autoproxy proxy-target-class="true"/>然后我按照这个方式结果老是包异常说什么。
后来找到原因是我工程里老的aspectjweaver-1.5的jar包没有删掉,删掉之后就ok了。现在整个项目的servlet.xml内容为:
<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true" >
<aop:advisor pointcut="execution(* com.best.staff.service.impl.*ServiceImpl.*(..))" advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* com.best.staff.controller.*Controller.*(..))" advice-ref="methodTimeAdvice" />
</aop:config>
<!-- 打印方法执行时间 -->
<bean id="methodTimeAdvice" class="com.best.staff.util.MethodTimeAdvice"/>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*,find*方法的事务为readonly,其余方法按默认设置.默认的设置请参考Spring文档事务一章. -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
发现当调用controller方法的时候根本不会进入,但是我把serviceImpl的advice-ref改成methodAdvice的时候发现能够进入执行对应的方法。然后从网上查了一下发现。
主要有两个解决方法:第一个是添加下载aspectj-1.6.10.jar和aspectjweaver-1.6.9 事实证明只对了一部分 第二个方法是修改servlet.xml去掉 <mvc:annotation-driven/>然后添加 <aop:aspectj-autoproxy proxy-target-class="true"/>然后我按照这个方式结果老是包异常说什么。
nested exception is java.lang.ClassCastException: org.aspectj.weaver.ResolvedType$Array cannot be cast to org.aspectj.weaver.ReferenceType
后来找到原因是我工程里老的aspectjweaver-1.5的jar包没有删掉,删掉之后就ok了。现在整个项目的servlet.xml内容为:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<mvc:annotation-driven/>
<!-- <context:annotation-config /> -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.best.staff.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value></value>
</property>
<property name="contentType" value="text/html;charset=utf8"></property>
</bean>
</beans>