Cleaner way to get new instance of an autowired field that is prototype in nature

╄→гoц情女王★ 提交于 2019-11-30 10:29:43

There is much better practice when you need to create Runnable/Callable and inject it into applicationContext it's called look up method:

Let's consider that all Runnable/Callable classes are @Prototype and @Lazy

@Component(value="task")
@Scope(value="prototype")
@Lazy(value=true)
public class Task implements Runnable {

public void run(){
.....
}

}

Now you need to Create Look up method factory:

    <bean id="taskFactory" class="x.y.z.TaskFactory">
<lookup-method name="createTask" bean="task"/>
</bean>

Now let's implement TaskFactory itself which is abstract class and have one abstract method :

@Component(value="taskFactory")
public abstract class TaskFactory {

    public abstract Task createTask();

}

Here comes the magic:

public class ThreadHandler{

@Autowired
private TaskFactory taskFactory;


public void someFunction(){
          Runnable task = taskFactory.createTask();
          taskExecutor.execute(task);
      }
}   

Every time you are calling createTask() method of taskFactory singleton object. you will receive completely new instance of your prototype object.

P.S: don't forget to add

<context:annotation-config />
    <context:component-scan base-package="x.y.z"></context:component-scan>

to enable Annotations correctly.

hope it Helps.

No, you don't get a new object, just by accessing a field that holds a reference to a bean scoped as Prototype. Spring doesn't have any way to replace your instance level reference with a new reference based just on field access. It's set once by the autowiring and then it's just a reference to that one object. If you want it to actually create a new one you need to use getBean as you observed.

You can tell Spring to override your get method and replace it with a 'getBean' using method injection, to get the Spring dependencies out of your bean:

<bean id="threadHandler" class="com.stackexchange.ThreadHandler">
<lookup-method name="getThreadName" bean="threadName"/>
</bean>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!