user1089362
user1089362

Reputation: 566

spring bean issue with Set property

I have bean e.g.

<bean id="manager" class="com.Manager" init-method="init">
       <property name="services">
        <set>
            <ref bean="service" />
        </set>
    </property>
</bean>

<bean id="myService"
        class="com.MyService" abstract="true">
</bean>

<bean id="service" class="com.SpecificService" parent="myService">

</bean>

service(SpecificService) is class which extends abstract class MyService with abstract method init() and implement interface MyInterface with method specificLogic().

So, manager calls method init() on service object like this:

private Set<MyService> services;

public void init() {

    for (MyService service : services) {
        service.init();
    }



}

But I have following problem when bean is initializing:

Failed to convert property value of type 'java.util.LinkedHashSet' to required type 'java.util.Set' for property 'services'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy108 implementing com.MyInterface,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.MyService] for property 'services[0]': no matching editors or conversion strategy found.

Upvotes: 0

Views: 1437

Answers (2)

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18383

Are you sure you have posted all your config? Because there is some AOP "hidden" (maybe by annotations) code around because service bean is proxied. You have to choice:

  1. Change declaration of private Set<MyService> services to `private Set services
  2. In your AOP code let the proxy expose the target-class (MyService) and not the interface (MyInterface)

Check your code

Upvotes: 1

razafinr
razafinr

Reputation: 932

Maybe you can try to initialize your Set services for the initialization.

You just declared it as

private Set<MyService> services;

It just considers the interface Set and in your bean configuration it seems like by default the set is a LinkedHashSet.

So you can try to initialize your Set services like :

 private Set<MyService> services = new LinkedHashSet<>();

Upvotes: 0

Related Questions