Spring prototype scope with autowired

Ayushi

I have a class JobListener that is listening to a queue using Spring Integration. Inside JobListener, i have an Autowired field Helper whose scope is defined as "prototype".

public class JobListener {

@Autowired
private Helper helper;

@ServiceActivator
public void receiveMessage(Message<String> message){
    helper.processMassage(message);
    }
}

Now my question is, Since the scope of Helper is defined as Protype, will i get a new instance of helper every time recieveMessage is called?

Debojit Saikia

The container only creates the singleton bean JobListener once, and thus only gets one opportunity to set the properties. The container cannot provide bean JobListener with a new instance of bean Helper every time one is needed.

One solution to this problem is to use Method Injection: Lookup method injection is the ability of the container to override methods on container managed beans, to return the lookup result for another named bean in the container. To implement this solution, re-define the JobListener class as this:

public abstract class JobListener {

@ServiceActivator
public void receiveMessage(Message<String> message){
    Helper helper = createHelper();
    helper.processMassage(message);
    }

protected abstract Helper createHelper();
}

The Spring Framework will generate a dynamic subclass of JobListener that will override the createHelper method to provide a new instance of Helper every time it is requested for.

You need to define the name of lookup-method name in the JobListener bean definition:

<bean id="helper" class="x.y.Helper" scope="prototype">
...
</bean>

<bean id="jobListener" class="x.y.JobListener">
<lookup-method name="createHelper" bean="helper"/>
</bean>

With the above configurations in place, every time you execute

Helper helper = createHelper();

it will return you a new instance of Helper.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When to use Spring prototype scope?

From Dev

Mystery around Spring Integration and prototype scope

From Dev

Spring bean scope. Singleton and Prototype

From Dev

Spring Batch Prototype Scope for Item Processor

From Dev

Prototype bean scope implementation in Spring Framework

From Dev

In Spring, how to declare a bean with the prototype scope?

From Dev

What is the difference between @lazy and @Scope("prototype") in Spring

From Dev

In Spring, why does a service with scope prototype instantiate many times?

From Dev

Spring scope=`prototype`: how to change all bean instances?

From Dev

Example of function prototype scope

From Dev

What is the difference between @Scope (BeanDefinition.SCOPE_PROTOTYPE) and @Scope (“prototype”)?

From Dev

When is an object of prototype bean gets garbage collected when we use proxy mode for Scope Protype in Spring Boot

From Dev

Spring Inheritance Autowired Annotations

From Dev

Spring: @Autowired not working with ApplicationContext

From Dev

@Autowired & Spring context hierarchy

From Dev

Spring 3 Autowired BeanInitializationException

From Dev

Spring Autowired null if not found

From Java

Understanding Spring @Autowired usage

From Dev

Spring pure annotation with autowired

From Dev

Singelton in Spring with Autowired field

From Dev

Spring @Autowired Failed

From Dev

Spring 4 @AutoWired Failed

From Dev

@Autowired in Spring PermissionEvaluator

From Dev

Spring bean created but not autowired

From Dev

Spring Boot Autowired null

From Dev

Spring @Autowired comes as null

From Dev

Spring @Autowired and Singletons

From Dev

Using @Autowired in Spring 3

From Dev

Specify order to Spring @Autowired

Related Related

HotTag

Archive