简体   繁体   English

Spring原型引用单例

[英]Spring Prototype refering Singleton

I am using ConcurrentTaskExecutor to run the multiple tasks at the same time. 我正在使用ConcurrentTaskExecutor运行多个任务。 By default in spring, it is scoped as singleton . 在春季默认情况下,其作用域为singleton

Here my taskExecutor is prototype and threadPoolExecutor is no.t 这里我taskExecutor样机 threadPoolExecutor是no.t

When requested a new taskExecutor is returned. 当请求时,将返回一个新的taskExecutor My question is, since we are referring a threadPoolExecutor from prototype, will threadPoolExecutor be a new instance? 我的问题是,由于我们从原型引用了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>

The "prototype" keyword just means that each time you ask Spring for an instance, you will get a fresh instance. “ prototype”关键字只是意味着每次您向Spring请求实例时,您都会获得一个新实例。 With the wiring you have, you are only asking once. 有了接线,您只需要问一次。 So I expect that your taskExecutor bean will be a single instance. 因此,我希望您的taskExecutor bean将是一个实例。

No, the prototype keyword does not have "deep" scope. 不, prototype关键字没有“深”范围。 It does not apply to referenced or inner beans. 它不适用于引用的或内部的bean。

For the bean marked as prototype, ie taskExecutor , Spring will create a new instance every time you ask for it. 对于标记为原型的bean,即taskExecutor ,每次您要求时,Spring都会创建一个新实例。 But all those new taskExecutors will contain the same singleton instance of threadPoolExecutor . 但是,所有这些新taskExecutors将包含相同的单一实例threadPoolExecutor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM