作用域:用于确定Spring创建Bean的实例个数。
单实例(singleton)的bean
这种单实例的是默认的类型
使用singleton定义的Bean是单例的,每次调用getBean都是调用的同一个对象。只要IOC容器一创建就会创建Bean的实例。
随便拿来一个Book类演示:
package com.Keafmd.spring5.collectiontype; import java.util.List; /** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:56 */ public class Book { private List<String> list; public void setList(List<String> list) { this.list = list; } public void test(){ System.out.println(list); } }
bean2.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--设置为单实例,不写默认的就是这种类型:scope="singleton"--> <!--设置为多实例:scope="prototype"--> <bean id="book" class="com.Keafmd.spring5.collectiontype.Book"> <property name="list" ref="bookList"></property> </bean> </beans>
测试代码:
package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 测试类 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void test(){ 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); } }
测试结果:
com.Keafmd.spring5.collectiontype.Book@376b4233 com.Keafmd.spring5.collectiontype.Book@376b4233 Process finished with exit code 0
输出结果相同证明是同一个对象,证明是单实例的。
多实例(prototype)的bean
设置为多实例需要用到scope属性,并且属性值设为"prototype"。这样就是多例的,每次通过Spring IOC容器获取prototype定义的Bean时,容器都将创建一个新的Bean实例。创建时不会实例该Bean,只有调用getBean方法时,才会实例化。
Book类:
package com.Keafmd.spring5.collectiontype; import java.util.List; /** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:56 */ public class Book { private List<String> list; public void setList(List<String> list) { this.list = list; } public void test(){ System.out.println(list); } }
修改bean2.xml的bean的scope的值为prototype。
bean2.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--设置为单实例,不写默认的就是这种类型:scope="singleton"--> <!--设置为多实例:scope="prototype"--> <bean id="book" class="com.Keafmd.spring5.collectiontype.Book" scope="prototype"> <property name="list" ref="bookList"></property> </bean> </beans>
测试代码:
package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 测试类 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void test(){ 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); } }
测试结果:
com.Keafmd.spring5.collectiontype.Book@376b4233 com.Keafmd.spring5.collectiontype.Book@2fd66ad3 Process finished with exit code 0
输出结果不同的,证明不是同一个对象,证明是多实例的。