struts2中使用spring的aop找不到方法有关问题

struts2中使用spring的aop找不到方法问题

今天开发一个Struts2+Spring+Ibatis的应用时,开始想用aop进行事务控制,但是发现+上aop后,在页面使用actionName!xxx的方式调用Action中指定的方法时,总是抛出NoSuchMethodException,找了很久,终于很久,终于解决了问题,在此记录,希望对以后有帮助.

解决方法:

      因为我是用Model-Driven模式实现Action,因此,实现了Action, ModelDriven接口,所以需要强制指定spring使用cblib来进行代理,不然就会找不到方法,代码如下:

    <bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource">
	    <ref local="dataSource" />
	</property>
    </bean>
  
    <aop:config proxy-target-class="true">  
       <aop:advisor pointcut="execution(* sample..*.*(..))" advice-ref="txAdvice"/>    
    </aop:config> 
    
    <tx:advice id="txAdvice"  transaction-manager="transactionManager">  
	   <tx:attributes>  
	     <tx:method name="save*"/>  
	     <tx:method name="add*"/>  
	     <tx:method name="insert*"/>
	     <tx:method name="remove*"/>
	     <tx:method name="del*"/>
	     <tx:method name="modify*"/>
	     <tx:method name="update*"/>
	     <tx:method name="import*"/>
	     <tx:method name="*" read-only="true"/>  
	   </tx:attributes>  
    </tx:advice> 

 

 注意突显部分,然后导入cblib的jar包把问题解决了.

 

spring对AOP的支持

1、如果目标对象实现了接口,默认会采用JDK的动态代理机制实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现接口,必须使用CGLIB生成代理,spring会自动在CGLIB和JDK动态代理之间切换

4.如何强制使用CGLIB生成代理?
* 添加CGLIB库,SPRING_HOME/lib/cglib/*.jar
* 在spring的配置文件中加入:
<aop:aspectj-autoproxy proxy-target-class="true"/>

JDK代理和CGLIB代理的区别?
* JDK代理只能对实现了接口的类生成代理,而不能针对类
* CGLIB是针对类实现代理的,主要对指定的类生成一个子类,并覆盖其中的方法,
  因为是继承,所以不能使用final来修饰类或方法<aop:aspectj-autoproxy proxy-target-class="true"/>