SpringIOC创建对象的单例和多例模式
单例模式(默认):
设置了单例模式的bean,会在Spring容器对象被创建的时候 就完成初始化创建,无论获取多少次都是同一个对象.
多例模式:
设置了多例模式的bean,在Spring容器对象被创建的时候不会被初 始化创建,每次获取的时候都会重新创建,每次获取的对象都不相同.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
SpringIOC设置对象的单例模式和多例模式创建对象
单例模式:默认模式,在bean标签上使用scope属性,默认值为singleton
多例模式:在bean标签上使用scope属性设置,值为prototype
-->
<bean id="stu" class="com.ann.pojo.Student" scope="singleton"></bean>
<bean id="tea" class="com.ann.pojo.Teacher" scope="prototype"></bean>
</beans>