wds
wds

Reputation: 32283

Wiring an ExecutorService in with Spring factory-method

I have a simple class that has a property called "executor" in which I'd like to wire in an ExecutorService, with spring 3.0. I followed the documentation which states you simply use the factory class (in this case Executors) and supply factory-method to create your service. However, when I try to wire the resulting bean into my class, it seems that spring thinks the class type is java.lang.String instead of ExecutorService.

I have no idea what I'm doing wrong here. I seem to be correct if I look at the documentation, but perhaps I need to somehow indicate the resulting class of calling the factory method?

Here's the error:

java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.concurrent.ExecutorService] for property 'executor': no matching editors or conversion strategy found

With this configuration:

<bean id="taskManager" class="examples.TaskManager">
    <property name="executor">
        <idref local="executorService" />
    </property>
</bean>
<bean id="executorService" class="java.util.concurrent.Executors" 
  factory-method="newSingleThreadExecutor" 
  destroy-method="shutdownNow" />

and this class:

public class TaskManager {

    private ExecutorService executor;

    public ExecutorService getExecutor() {
        return executor;
    }

    public void setExecutor(ExecutorService executor) {
        this.executor = executor;
    }   
}

Upvotes: 1

Views: 13286

Answers (2)

yair
yair

Reputation: 9245

Try simple ref attribute instead of idref:

<bean id="taskManager" class="examples.TaskManager">
    <property name="executor" ref="executorService" />
</bean>

and see here that the meaning of idref is passing the referenced bean's name (as a String) to the property.

Oh, and here's the Spring doc itself:

The idref element

The idref element is simply an error-proof way to pass the id (string value - not a reference) of another bean in the container to a or element.

Upvotes: 7

Hemant Metalia
Hemant Metalia

Reputation: 30638

use ref instead of idref idref returns the string name of the passed argument

<bean id="taskManager" class="examples.TaskManager">
    <property name="executor" ref="executorService" />
  </bean>

The idref element in spring framework

The idref element allow you to pass the bean id (which is the string value not the reference) of another bean in the container to a or . In the given example it is clearly shown how pass the bean id to another bean and also display the bean id

refer http://www.roseindia.net/tutorial/spring/spring3/ioc/springidrefelement.html

Reference Injection

In the reference injection one bean definition is injected to another. For reference injection you use the constructor-arg or property's ref attribute instead of the value attribute.

refer http://www.roseindia.net/tutorial/spring/spring3/ioc/springreferenceinjection.html

Upvotes: 1

Related Questions