我在项目中用到了泛型,也主要是为了学习这方面知识,但是在配置SPRING的时候总是有错,请各位指点一下
下面是applicationContext-service.xml的配置
<?xml version="1.0" encoding="utf-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="baseTxProxy" lazy-init="true" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="accountInfo" class="com.abc.AccountInfo"></bean>
<bean id="accountInfoImpl" parent="baseTxProxy" class="com.abc.impl.AccountInfoImpl">
<constructor-arg><ref bean="accountInfo"/></constructor-arg>
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="accountInfoAction" scope="prototype" class="com.abc.controller.AccountInfoAction">
<property name="accountInfoImpl">
<ref bean="accountInfoImpl"/>
</property>
</bean>
</beans>下面是错误提示
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountInfoImpl' defined in file [D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\classes\com\dscver\xml\spring\applicationContext-service.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class]: Could not convert constructor argument value of type [com.abc.AccountInfo] to required type [java.lang.Class]: Failed to convert value of type [com.abc.AccountInfo] to required type [java.lang.Class]; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.abc.AccountInfo] to required type [java.lang.Class]: no matching editors or conversion strategy found
下面是applicationContext-service.xml的配置
<?xml version="1.0" encoding="utf-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="baseTxProxy" lazy-init="true" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="accountInfo" class="com.abc.AccountInfo"></bean>
<bean id="accountInfoImpl" parent="baseTxProxy" class="com.abc.impl.AccountInfoImpl">
<constructor-arg><ref bean="accountInfo"/></constructor-arg>
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="accountInfoAction" scope="prototype" class="com.abc.controller.AccountInfoAction">
<property name="accountInfoImpl">
<ref bean="accountInfoImpl"/>
</property>
</bean>
</beans>下面是错误提示
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountInfoImpl' defined in file [D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\classes\com\dscver\xml\spring\applicationContext-service.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class]: Could not convert constructor argument value of type [com.abc.AccountInfo] to required type [java.lang.Class]: Failed to convert value of type [com.abc.AccountInfo] to required type [java.lang.Class]; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.abc.AccountInfo] to required type [java.lang.Class]: no matching editors or conversion strategy found
因此抛出如下异常
org.springframework.beans.factory.UnsatisfiedDependencyException
解决办法:
建立一个没有该参数的构造器或写入参数
<bean id="accountInfo" class="com.abc.AccountInfo"> </bean>
<bean id="accountInfoImpl" parent="baseTxProxy" class="com.abc.impl.AccountInfoImpl">
<constructor-arg> <ref bean="accountInfo"/> </constructor-arg>
<property name="hibernateTemplate" ref="hibernateTemplate"> </property>
</bean>
你发的代码不全
自己先检查一下这段代码
AccountInfoImpl中有构造方法
public AccountInfoImpl(Class<AccountInfo> type){
super(type);
}
这个类继承了一个通用的IMPL类,这个类中有下面的构造方法
public GenericServiceImpl(Class<T> type){
this.type = type;
}
this.type = type;
}
问题应该在这
Class <T> type
所以jvm在运行期根本不管是不是generic,
编译后的类和无泛型的没什么区别.
spring不支持泛型检查.
这只是个例子如果在applicationContext-service.xml中写:
<bean id="aaa" class = "TestImplementation">
</bean>
在类中就写成:
Test<String> aaa;