问题描述:
接口A提供公用的CRUD 操作,抽象类B实现接口A,接口C提供各模型特有的相关数据库操作。类D继承抽象类B实现接口C如果使用JDK动态代理,Spring会将类D向上转型为接口C,那么在使用中必须注入接口A。如果注入类D的类型。会报错说代理类型XX无法转换为类D的类型。解决办法如下:
<bean id="transactionManagerCMS"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
<property name="dataSource" ref="dataSourceCMS" />
</bean>
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean
below) -->
<tx:advice id="txAdvice" transaction-manager="transactionManagerCMS">
<!-- the transactional semantics... -->
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution of
an operation defined by the FooService interface -->
<aop:config proxy-target-class="true">
<aop:pointcut id="fooServiceOperation"
expression="execution(* com.harvest.smartHotels.info.dao.serviceContent.interfaces.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>
动态代理使用CGLIB,这样注入类型就可以使类D。