您的位置:首页 > 编程语言 > Java开发

Spring的AOP实现使用的JDK的动态代理必须使用接口

2014-12-26 14:18 941 查看
       今天,在项目中遇到一个问题,情况是这样的:在一个项目中,我配置了一个用以处理任务的工厂类,然后将其他的service类注入到这个工厂类中进行使用。在Spring中的配置如下:

<bean id="linkingDetectService" class="cn.vobile.service.linkingdetect.LinkingDetectServiceImpl">
<property name="taskPriority" value="normal" />
<property name="commonTaskService" ref="commonTaskService" />
<property name="gson" ref="gson" />
</bean>

<bean id="reclaimMatchSubscriptionService" class="cn.vobile.service.matchsubscription.reclaim.ReclaimMatchSubscriptionServiceImpl">
<property name="taskPriority" value="normal" />
<property name="matchSubscriptionService" ref="matchSubscriptionService"/>
<property name="gson" ref="gson" />
<property name="matchedVideoService" ref="matchedVideoCommonService" />
<property name="transactionTemplate" ref="masterTransactionTemplate" />
</bean>

<bean id="commonTaskFactory" class="cn.vobile.service.commontask.CommonTaskFactory">
<property name="serviceMap">
<map>
<entry key="linkingDetect"><ref local="linkingDetectService"/></entry>
<entry key="reclaimMatchSubscription"><ref local="reclaimMatchSubscriptionService"/></entry>
</map>
</property>
</bean>
结果在项目其中的时候就报了一个配置错误,如下所示:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'matchedVideoService' defined in ServletContext resource [/WEB-INF/classes/applicationServiceContext.xml]: Cannot resolve reference to bean 'commonTaskFactory' while setting bean property 'commonTaskFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'commonTaskFactory' defined in ServletContext resource [/WEB-INF/classes/applicationServiceContext.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.


错误显示是动态代理部分出现了问题,需要使用CGLIB来解决,这时候我想到了项目中是对service层配置了AOP事务的,配置如下:

<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="masterTransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS" read-only="true" timeout="20"/>
<tx:method name="is*" propagation="SUPPORTS" read-only="true" timeout="20"/>
<tx:method name="has*" propagation="SUPPORTS" read-only="true" timeout="20"/>
<tx:method name="exist*" propagation="SUPPORTS" read-only="true" timeout="20"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="vtServiceTransaction"
expression="execution(* cn.vobile.service.*.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="vtServiceTransaction" />
</aop:config>


出现该错误的原因是:Spring的AOP默认使用JDK的动态代理实现的,JDK的动态代理只能代理接口中的方法(是针对接口生成代理类),像这里直接使用***Factory类进行注入是不行的,而CGLIB是生成子类,所以可以不用提供接口。

解决方案一:引入CGLIB的jar包。

解决方案二:把这里的***Factory类从事务层挪出去,这样就不会调用JDK的动态代理了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息