Spring6 -(04)Bean 的作用域
1. singleton
默认情况下,Spring的IoC容器创建的Bean对象是单例的。
第一步:创建SpringBean类
package com.julissa.spring6.bean;
public class SpringBean {
}
第二步:配置beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置SprigBean -->
<!-- 默认:singleton -->
<bean id="springBean" class="com.julissa.spring6.bean.SpringBean"/>
</beans>
第三步:编程测试程序
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
System.out.println("第一个SpringBean对象:" + springBean);
SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
System.out.println("第二个SpringBean对象:" + springBean2);
}
第四步:运行测试程序
通过测试得知:Spring的IoC容器中,默认情况下,Bean对象是单例的。
这个对象在什么时候创建的呢?可以为SpringBean提供一个无参数构造方法,测试一下
第一步:修改SpringBean类
package com.julissa.spring6.bean;
public class SpringBean {
public SpringBean() {
System.out.println("SpringBean的无参构造方法执行了。");
}
}
第二步:编写测试程序
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
}
第三步:运行测试程序
通过测试得知,默认情况下,Bean对象的创建是在初始化Spring上下文的时候就完成的。
要点:
- 默认清空下Bean是单例的(单例:singleton)
- 在初始化Spring上下文的时候实例化
- 每一次调用getBean()方法的的时候,都返回那个单例的对象
2. prototype
如果想让Spring的Bean对象以多例的形式存在,可以在bean标签中指定scope属性的值为:prototype,
这样Spring会在每一次执行getBean()方法的时候创建Bean对象,调用几次则创建几次。
第一步:创建SpringBean类
package com.julissa.spring6.bean;
public class SpringBean {
public SpringBean() {
System.out.println("SpringBean的无参构造方法执行了。");
}
}
第二步:配置beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置SprigBean -->
<!-- prototype -->
<bean id="springBean" class="com.julissa.spring6.bean.SpringBean" scope="prototype"/>
</beans>
第三步:编程测试程序
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
System.out.println("第一个SpringBean对象:" + springBean);
SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
System.out.println("第二个SpringBean对象:" + springBean2);
}
第四步:运行测试程序
把测试代码中的getBean()方法所在行代码注释掉:
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
}
运行测试程序
可以看到这一次在初始化Spring上下文的时候,并没有创建Bean对象。
那你可能会问:scope如果没有配置,它的默认值是什么呢?默认值是singleton,单例的。
修改beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置SprigBean -->
<bean id="springBean" class="com.julissa.spring6.bean.SpringBean" scope="singleton"/>
</beans>
编写测试程序
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
System.out.println("第一个SpringBean对象:" + springBean);
SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
System.out.println("第二个SpringBean对象:" + springBean2);
}
运行测试程序
通过测试得知,没有指定scope属性时,默认是singleton单例的。
要点:
- 当将Bean的scope属性设置为prototype,这时Bean是多例的
- Spring初始化上下文的时候,并不会实例化bean对象
- 每一次执行getBean()方法的时候实例化Bean对象
3. 其他scope
scope属性的值不止两个,它一共包括8个选项:
- singleton:默认的,单例。
- prototype:原型。每调用一次getBean()方法则获取一个新的Bean对象。或每次注入的时候都是新对象。
- request:一个请求对应一个Bean。仅限于在WEB应用中使用。
- session:一个会话对应一个Bean。仅限于在WEB应用中使用。
- global session:portlet应用中专用的。如果在Servlet的WEB应用中使用global session的话,和session一个效果。(portlet和servlet都是规范。servlet运行在servlet容器中,例如Tomcat。portlet运行在portlet容器中。)
- application:一个应用对应一个Bean。仅限于在WEB应用中使用。
- websocket:一个websocket生命周期对应一个Bean。仅限于在WEB应用中使用。
- 自定义scope:很少使用。