1.Spring配置文件中bean标签有一个属性scope,用于设置单实例对象还是多实例对象
2.scope属性值:
第一个值默认值为singleton,用于设置单实例对象
第二个值 prototype,用于设置多实例对象
3.singleton与prototype的区别
第一:singleton为单实例对象,prototype为多实例对象
第二:设置scope值是singleton时,加载配置文件的时候就会创建单实例对象
设置scope值时prototype时,在调用getBean时会创建多实例对象
4.单实例对象示例代码
<bean id="book" class="com.ljx.test.bean.Book" scope="singleton">
</bean>
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
Book book1 = context.getBean("book", Book.class);
Book book2=context.getBean("book",Book.class) ;
System.out.println(book1);
System.out.println(book2);
}
5.多实例对象示例代码
<bean id="book" class="com.ljx.test.bean.Book" scope="prototype">
</bean>
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
Book book1 = context.getBean("book", Book.class);
Book book2=context.getBean("book",Book.class) ;
System.out.println(book1);
System.out.println(book2);
}