连接executorService时,不明确的工厂方法参数类型错误

时间:2013-10-10 21:32:37

标签: java spring

我有一个bean定义:

<bean id="executor" class="java.util.concurrent.Executors" 
      factory-method="newSingleThreadExecutor" destroy-method="shutdownNow" />

加载时,不幸导致:

Ignoring factory method [public static java.util.concurrent.ExecutorService java.util.concurrent.Executors.newSingleThreadExecutor(java.util.concurrent.ThreadFactory)] of bean 'executor': 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'executor' defined in class path resource [main-config.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.concurrent.ThreadFactory]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?

Spring似乎想要使用newSingleThreadExecutor(ThreadFactory)方法,而我只想使用无参数方法。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我不知道您使用的是哪个版本的Spring,but every document for versions above 3.0 state要为静态工厂方法提供参数,您需要使用<constructor-arg>元素中的<bean>

指定

<bean id="executor" class="java.util.concurrent.Executors" 
  factory-method="newSingleThreadExecutor" destroy-method="shutdownNow" />

将使用no-arg方法newSingleThreadExeutor()