我有以下的类:
class Foo<KeyType, ValueType> {
private Producer<KeyType, ValueType> kafkaProducer;
public Foo() {
this.kafkaProducer = new Producer<KeyType, ValueType>(new ProducerConfig());
}
}
有另一个DAO类使用了这个Foo类,它看起来如下所示:
class CompanyDao {
@Autowired
private Foo<String, Integer> fooHelper;
}
我希望Spring可以在fooHelper
对象中注入Foo
类型的对象。为此,我使用以下XML配置:
<bean id="fooHelper" class="com.ask.util.Foo">
<property name="KeyType" value="java.lang.String" />
<property name="ValueType" value="Integer" />
</bean>
<bean id="CompanyDao" class="com.ask.dao.CompanyDao">
<property name="fooHelper"><ref bean="fooHelder"/></property>
</bean>
当我使用这个XML配置时,Spring抛出以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooHelper' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'KeyType' of bean class [com.ask.util.fooHelper]: Bean property 'KeyType' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
有什么办法可以解决这个错误吗?
KeyType
和ValueType
两个属性:<bean id="fooHelper" class="com.ask.util.Foo" />
。 - Tunaki