Spring Prototype refering Singleton

891 Views Asked by At

I am using ConcurrentTaskExecutor to run the multiple tasks at the same time. By default in spring, it is scoped as singleton.

Here my taskExecutor is prototype and threadPoolExecutor is no.t

When requested a new taskExecutor is returned. My question is, since we are referring a threadPoolExecutor from prototype, will threadPoolExecutor be a new instance?

<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>
2

There are 2 best solutions below

0
On BEST ANSWER

The "prototype" keyword just means that each time you ask Spring for an instance, you will get a fresh instance. With the wiring you have, you are only asking once. So I expect that your taskExecutor bean will be a single instance.

0
On

No, the prototype keyword does not have "deep" scope. It does not apply to referenced or inner beans.

For the bean marked as prototype, i.e. taskExecutor, Spring will create a new instance every time you ask for it. But all those new taskExecutors will contain the same singleton instance of threadPoolExecutor.