我正在使用ConcurrentTaskExecutor
同时运行多个任务。默认情况下,在 Spring 中,它的作用域为单例。
这里我的taskExecutor
是原型(prototype),而threadPoolExecutor
不是。
当请求时,返回一个新的taskExecutor
。我的问题是,既然我们从原型(prototype)引用一个 threadPoolExecutor
,那么 threadPoolExecutor
会是一个新实例吗?
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ConcurrentTaskExecutor" scope="prototype">
<property name="concurrentExecutor" ref="threadPoolExecutor"/>
</bean>
<bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="100" />
<property name="maxPoolSize" value="200" />
<property name="queueCapacity" value="1000" />
</bean>
最佳答案
“prototype”关键字只是意味着每次你向 Spring 请求一个实例时,你都会得到一个新的实例。利用您现有的接线,您只需询问一次。所以我希望您的 taskExecutor bean 将是一个实例。
关于java - Spring 原型(prototype)引用 Singleton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24547873/