什么是bean得作用域?
在spring里边,设置得bean实例是单实例还是多实例
在spring里,默认情况下 bean是单实例对象
@Test public void testCollection2(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); Book book = context.getBean("book",Book.class); Book book2 = context.getBean("book",Book.class); System.out.println(book); System.out.println(book2); }
地址相同 单实例对象
如何设置单实例和多实例?
使用scope属性值
第一个值 默认值 singleton
第二个值 多实例 prototype
singleton 单实例 prototype 多实例得区别
设置scope值是singleton时候,加载spring配置文件时就会创建单实例对象
设置scope得值是prototype时候,在调用getbean方法得时候就会创建多实例对象