今天做两个项目的集成,主项目单独跑没问题,子项目单独跑没问题,集成到一起就挂了。报错信息为:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [$Proxy121] to required type
[com.**.facilities.group.service.BaseFacilitiesGroupService]
for property 'baseFacilitiesGroupService'; nested exception is java.lang.IllegalArgumentException:
Cannot convert value of type [$Proxy121] to required type
[com.**.facilities.group.service.BaseFacilitiesGroupService]
for property 'baseFacilitiesGroupService': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'baseFacilitiesGroupForVoService' defined in ServletContext resource [/WE
B-INF/classes/spring/facilities.xml]: Cannot resolve reference to bea
n 'baseFacilitiesService' while setting bean property 'baseFacilitiesService'; n
ested exception is org.springframework.beans.factory.BeanCreationException: Erro
r creating bean with name 'baseFacilitiesService' defined in ServletContext reso
urce [/WEB-INF/classes/spring/facilities.xml]: Cannot resolve referen
ce to bean 'baseFacilitiesServiceTarget' while setting bean property 'target'; n
ested exception is org.springframework.beans.factory.BeanCreationException: Erro
r creating bean with name 'baseFacilitiesServiceTarget' defined in ServletContex
t resource [/WEB-INF/spring/facilities.xml]: Error setting pr
operty values; nested exception is org.springframework.beans.PropertyBatchUpdate
Exception; nested PropertyAccessExceptions (1) are:
从报错信息来看,是类注入的时候,类型不匹配。再看一下,service是经过了一层事务代理的。看了一下两个项目的配置,完全没有问题。配置文件如下:
<bean id="baseFacilitiesGroupService" parent="baseTransactionProxy"> <property name="target" ref="baseFacilitiesGroupServiceTarget"/> </bean> <bean id="baseFacilitiesGroupServiceTarget" class="com.**.BaseFacilitiesGroupService"> <property name="baseFacilitiesGroupDao" ref="baseFacilitiesGroupDao"/> </bean> <!--事务管理代理--> <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager" /> <property name="proxyTargetClass"> <value>true</value> </property> <property name="transactionAttributes"> <props> <!--<prop key="**">PROPAGATION_REQUIRED,-Exception</prop> --> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean>
从网上搜索错误说proxyTargetClass需要配置为true,我们也配置了。
参考:http://blog.chinaunix.net/u/20045/showart_1019030.html
后来听说主项目没有使用我们的配置方法。而是使用了:
<bean id="chainService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target"> <ref bean="chainServiceTarget"/> </property> <property name="transactionAttributes"> <props> <prop key="create*"> PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED </prop> </property> </bean>
根据这种方式修改后,还是不work,后来想到,感觉是不是应该吧proxyTargetClass=true也配置上,加上之后,果然work了。
<bean id="chainService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="proxyTargetClass"> <value>true</value> </property> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target"> <ref bean="chainServiceTarget"/> </property> <property name="transactionAttributes"> <props> <prop key="create*"> PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED </prop> </props> </property> </bean>
再后来看到了如下的文章:ProxyFactoryBean什么时候需要配置 proxyTargetClass
说是spring动态代理使用cglib的时候会通过类所实现的接口动态代理生成类。这下去看那个报错的类,他实现了接口,但是其他地方使用的时候却是直接使用了这个类。所以动态代理生成的类,是会产生类型不匹配的问题。所以问题找到。
但是仍然有遗留的问题:
1.为什么子项目单独是可以运行的?
2.为什么我们最后配置完之后,其实就是将parent取掉,然后把代理的配置移到了本身上面,这两种方式我认为是没有区别的。为啥一个可以work,一个不行。
有谁可以解答一下,谢谢。