创建 bean 时出错,无法将新的 LinkedBlockingQueue 作为构造函数注入到 ThreadPoolExecutor
我正在尝试创建一个 ThreadPoolExecutor bean,它需要作为属性传递给许多其他 bean。
我已经定义了以下内容,但在创建 LinkedBlockingQueue 时遇到了一个奇怪的错误(见下文)。
<bean name="moduleExecutorService" class="java.util.concurrent.ThreadPoolExecutor">
<constructor-arg value="1" /> <!-- Minimun # of threads in pool -->
<constructor-arg value="20" /> <!-- Maximum # of threads in pool (pool is a caching pool that will only keep open those necessary) -->
<constructor-arg value="60" /> <!-- Shutdown unused threads after this TimeUnit -->
<constructor-arg>
<bean class="java.util.concurrent.TimeUnit" factory-method="valueOf">
<constructor-arg value="SECONDS" />
</bean>
</constructor-arg>
<constructor-arg type="java.util.concurrent.LinkedBlockingQueue"><bean class="java.util.concurrent.LinkedBlockingQueue" /></constructor-arg>
</bean>
错误:
446 [main] DEBUG org.springframework.beans.TypeConverterDelegate - Cannot create copy of Collection type [java.util.concurrent.LinkedBlockingQueue] - injecting original Collection as-is
java.lang.InstantiationException: java.util.concurrent.BlockingQueue
I'm trying to create a ThreadPoolExecutor bean which needs to be passed as a property to a number of other beans.
I have defined the following, but I'm encountering an odd error creating the LinkedBlockingQueue (seen below).
<bean name="moduleExecutorService" class="java.util.concurrent.ThreadPoolExecutor">
<constructor-arg value="1" /> <!-- Minimun # of threads in pool -->
<constructor-arg value="20" /> <!-- Maximum # of threads in pool (pool is a caching pool that will only keep open those necessary) -->
<constructor-arg value="60" /> <!-- Shutdown unused threads after this TimeUnit -->
<constructor-arg>
<bean class="java.util.concurrent.TimeUnit" factory-method="valueOf">
<constructor-arg value="SECONDS" />
</bean>
</constructor-arg>
<constructor-arg type="java.util.concurrent.LinkedBlockingQueue"><bean class="java.util.concurrent.LinkedBlockingQueue" /></constructor-arg>
</bean>
Error:
446 [main] DEBUG org.springframework.beans.TypeConverterDelegate - Cannot create copy of Collection type [java.util.concurrent.LinkedBlockingQueue] - injecting original Collection as-is
java.lang.InstantiationException: java.util.concurrent.BlockingQueue
我不太确定为什么会出现此消息。最后,它只是一条
DEBUG
消息,它不应该阻止您启动应用程序。或者,您可以使用FactoryBean
来实例化ThreadPoolExecutor
。然后,在您的应用程序上下文中,您将像这样创建 bean:
I am not quite sure why this message shows up. In the end it's only a
DEBUG
message and it should not prevent you from bringing up your application. Alternatively, you could use aFactoryBean
instead that instantiatesThreadPoolExecutor
.In your application context you'd then create the bean like this: