我必须在以XML编写的Spring配置文件中创建ScheduledThreadPoolExecutor
的bean。在Java中,我将其写为
@Bean(name = "MyThreadPoolList")
public List<ScheduledThreadPoolExecutor> executorList() {
List<ScheduledThreadPoolExecutor> executor = new ArrayList<>();
ScheduledThreadPoolExecutor tempExecutor;
for (int i = 0; i< 3; i++ ){
tempExecutor = new ScheduledThreadPoolExecutor(
1,
NamingThreadFactory.createWithDaemon("ExecutorPool-"+i+"-Thread-%d", true));
tempExecutor.prestartAllCoreThreads();
executor.add(i, tempExecutor);
}
return Collections.unmodifiableList(executor);
}
但是我不知道用XML实现此目标的最佳方法是什么。我可以尝试以下操作:
<bean id="MyThreadPoolList" class="java.util.ArrayList">
<constructor-arg>
<list>
<bean class="java.util.concurrent.ScheduledThreadPoolExecutor" init-method="prestartAllCoreThreads">
<constructor-arg value="1"/>
<constructor-arg>
<bean class="my.factory.NamingThreadFactory" factory-method="createWithDaemon">
<constructor-arg value="ExecutorPool-0-Thread-%d"/>
<constructor-arg type="boolean" value="true"/>
</bean>
</constructor-arg>
</bean>
<bean class="java.util.concurrent.ScheduledThreadPoolExecutor" init-method="prestartAllCoreThreads">
<constructor-arg value="1"/>
<constructor-arg>
<bean class="my.factory.NamingThreadFactory" factory-method="createWithDaemon">
<constructor-arg value="ExecutorPool-1-Thread-%d"/>
<constructor-arg type="boolean" value="true"/>
</bean>
</constructor-arg>
</bean>
<bean class="java.util.concurrent.ScheduledThreadPoolExecutor" init-method="prestartAllCoreThreads">
<constructor-arg value="1"/>
<constructor-arg>
<bean class="my.factory.NamingThreadFactory" factory-method="createWithDaemon">
<constructor-arg value="ExecutorPool-2-Thread-%d"/>
<constructor-arg type="boolean" value="true"/>
</bean>
</constructor-arg>
</bean>
</list>
</constructor-arg>
</bean>
但是问题是:
有没有更优雅的方法来实现这一目标?