當我在BeanFactory
上調用getBean(name)
時,我找回了在應用程序上下文中定義的bean實例。但是,當我再次調用getBean(name)
(使用相同的名稱)時,我得到了相同的bean實例。我理解這在一些(很多?)情況下是如何可取的,但我怎麼告訴BeanFactory
給我一個新的實例?如何強制spring容器不返回bean的單例實例?
示例Spring配置(簡潔...我漏掉了一些詳細說明,但這應該得到跨越點):當我運行這個
for(int i = 0;i++;i<=1) {
ApplicationContext context = ClassPathXmlApplicationContext("context.xml");
Object o = context.getBean("beanA");
System.out.println(o.toString()); // Note: misc.BeanA does not implement
// toString(), so this will display the OOID
// so that we can tell if it's the same
// instance
}
:
<beans>
<bean id="beanA" class="misc.BeanClass"/>
</beans>
實例的Java ,我得到這樣的東西:
[email protected]
[email protected]
請注意,兩者具有相同的OOID ...所以這些都是相同的實例......但我想要不同的實例。
這樣做了。有兩件事讓我感到困惑:1.最初,我在尋找getBean(String)的參數,而不是配置中的屬性... 2.在Spring 1.x(我以前的春天體驗)中,該屬性被稱爲「單身「,但這顯然在2.5中不起作用。 – Jared 2009-01-14 21:05:07