注意:JDK的动态代理,只能对实现接口的类实现代理,生成代理对象,如果这个类没有实现接口,是生成不了代理对象的。如本例UserManagerImpl实现了UserManager,如果去掉了UserManager接口,会出现异常(找不到cglib库)。
要解决这个问题,要添加cglib库,即:/spring_home/cglib/cglib-nodep-2.1_3.jar
如果目标对象实现了接口,默认情况下会使用JDK的动态代理实现AOP;如果有些目标对象没有实现接口,而有些又实现了,那么框架会在jdk动态代理和cglib直接灵活切换,实现了接口的使用动态代理,没有实现接口的采用cglib,因此较为保险的做法是总是引入cglib库。
如下面的代码, Spring注入的是接口,关联的是实现类。 这里注入了实现类,所以报异常了。 (出现在事务处理过程中)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [E:\JavaProj\Hibernate_Spring_Study\WebRoot\WEB-INF\classes\applicationContext_beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing com.jdbcdemo.dao.LogDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.jdbcdemo.dao.impl.LogDaoImpl] for property 'logDaoImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing com.jdbcdemo.dao.LogDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.jdbcdemo.dao.impl.LogDaoImpl] for property 'logDaoImpl': no matching editors or conversion strategy found<beanid="aa"class="com.jdbcdemo.util.HibernateUtil"></bean> <beanid="ld"class="com.jdbcdemo.dao.impl.LogDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <beanid="userDao"class="com.jdbcdemo.dao.impl.UserDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> <propertyname="logDaoImpl"ref="ld"></property> </bean>
______________________________________________________________________________________________
解决办法1, <aop:config proxy-target-class="true"> ,表示可以用类注入:
解决办法2,在 tx:annotation-driven中表示可以用类注入 :<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <propertyname="sessionFactory"> <refbean="sessionFactory"/> </property> </bean> <tx:annotation-driventransaction-manager="transactionManager"/> <tx:adviceid="txAdvice"> <tx:attributes> <tx:methodname="add"/> </tx:attributes> </tx:advice> <aop:configproxy-target-class="true"> <aop:pointcutexpression="execution(* com.jdbcdemo.dao.impl.*.*(..))"id="allManagerMethod"/> <aop:advisoradvice-ref="txAdvice"pointcut-ref="allManagerMethod"/> </aop:config> </beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="true"/>
<tx:advice id="txAdvice" >
<tx:attributes>
<tx:method name="add"/>
</tx:attributes>
</tx:advice>
<aop:config >
<aop:pointcut expression="execution(* com.jdbcdemo.dao.impl.*.*(..))"id="allManagerMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>