Spring: Create new instance of bean for each call of get method

烈酒焚心 提交于 2019-11-30 11:17:54

You are looking for lookup method functionality in Spring. The idea is that you provide an abstract method like this:

@Component
public abstract class Test {
  public abstract DataBean getBean();
}

And tell Spring that it should implement it at runtime:

<bean id="test" class="com.test.Test">
  <lookup-method name="getBean" bean="dataBean"/>
</bean>

Now every time you call Test.getBean you will actually call Spring-generated method. This method will ask ApplicationContext for DataBean instance. If this bean is prototype-scoped, you will get new instance each time you call it.

I wrote about this feature here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!