3

I have class like below :

class Foo<KeyType, ValueType> {
    private Producer<KeyType, ValueType> kafkaProducer;

    public Foo() {
        this.kafkaProducer = new Producer<KeyType, ValueType>(new ProducerConfig());
    }
}

There is another DAO class which uses this Foo class which looks like below :

class CompanyDao {
    @Autowired
    private Foo<String, Integer> fooHelper;
}

I want Spring to inject object of type Foo in fooHelpder object. For this I am using following XML configuration :

<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>

When I use this XML configurtion, Spring throws following error :

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)

Any idea how to resolve this error?

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Shekhar
  • 11,438
  • 36
  • 130
  • 186

2 Answers2

2

There are two changes needed.

First one because Spring 4 is now using Generics during injection (Pre Spring 4 version ignored generics):

class CompanyDao {
    private Foo<KeyType, ValueType> fooHelper;
}

(annotation is not needed when you are using XML config)

and

<bean id="fooHelper" class="com.ask.util.Foo">
</bean>
<bean id="CompanyDao" class="com.ask.dao.CompanyDao">
    <property name="fooHelper"><ref bean="fooHelder"/></property>
</bean>
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
0

Add KeyType and ValueType attributes to your class with setter methods.

Rahul Yadav
  • 1,503
  • 8
  • 11