如何强制弹簧容器不返回豆的单例实例?
当我调用 时,我会取回在应用程序上下文中定义的 Bean 的实例。但是,当我再次调用(具有相同的名称)时,我会得到相同的bean实例。我理解这在某些(许多?)情况下是可取的,但是我如何告诉他们给我一个新的实例?getBean(name)
BeanFactory
getBean(name)
BeanFactory
弹簧配置示例(简洁...我省略了一些冗长的内容,但这应该能说明这一点):
<beans>
<bean id="beanA" class="misc.BeanClass"/>
</beans>
示例 Java:
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
}
当我运行这个时,我得到这样的东西:
misc.BeanClass@139894
misc.BeanClass@139894
请注意,两者具有相同的OOID...所以这些是相同的实例...但我想要不同的实例。