白话Spring(基础篇)---bean的作用域

来源:互联网 发布:淘宝联盟怎么注册账号 编辑:程序博客网 时间:2024/05/21 14:48

[一知半解,就是给自己挖坑]

对于bean的作用域,我们在前文中已经提及了常用的singleton,prototype两种模式。本文我们将全面的介绍Spring中bean的各种作用域。不过,正如我们前文举的例子,最最常用的也是这两个,对于接下来即将介绍的作用域配置。读者可以按照自己的需求来选择使用

a.操作系统:win7 x64

b.开发工具:eclipse mars j2ee版本,maven3.3.2,Spring 4,junit4.12

c.复制Spring07工程,重命名为Spring08工程。修改工程结构图如下:


--------------------------------------------------------------------------------------------------------------------------------------------------------

一,默认的singleton作用域:在每个Spring IoC容器中一个bean定义只对应一个对象实例。这个对象实例将会被保存到缓存中,后续对该bean的引用和请求都返回的是该对象实例。

1.修改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"><bean id="Customer1" class="com.java.ingo.entity.Customer"><property name="name" value="Tom"></property><property name="sex" value="male"></property><property name="age" value="22"></property></bean></beans>
2.修改单元测试方法如下:

package com.java.ingo.test;import org.junit.After;import org.junit.Before;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.java.ingo.entity.Customer;/** * @author 作者 E-mail:ingo * @version 创建时间:2016年2月29日上午19:22:47 类说明 */public class Test {private ApplicationContext ac;@Beforepublic void setUp() throws Exception {System.out.println("......junit单元测试是方法之前执行......");// 首先读取配置文件,配置文件中的bean将会保存到ApplicationContext的实例中ac = new ClassPathXmlApplicationContext("beans.xml");}@Afterpublic void tearDown() throws Exception {System.out.println("......junit单元测试是方法之后执行......");}@org.junit.Testpublic void test1() {System.out.println("Start Test1()");Customer customer1 = (Customer)ac.getBean("Customer1");System.out.println(customer1.hashCode());Customer customer2 = (Customer)ac.getBean("Customer1");System.out.println(customer2.hashCode());}}
3.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。

结论:这里我们看到customer1与customer2是对应同一个对象,即在默认情况下,Spring对每一个bean只创建一个实例,程序获取的都是这个实例的引用

--------------------------------------------------------------------------------------------------------------------------------------------------------
二,prototype作用域:每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例。等价于一个new动作。这里需要注意的是:对于配置为prototype的bean,Spring只负责创建,而不管删除。后续的对该对象的生命周期维护就交给了客户端。

1.修改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"><bean id="Customer1" class="com.java.ingo.entity.Customer" scope="prototype"><property name="name" value="Tom"></property><property name="sex" value="male"></property><property name="age" value="22"></property></bean></beans>
2.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。
结论:这里我们看到customer1与customer2是不同的对象。如果读者有在Customer的构造函数中输出内容的话,那么控制台也可以看到构造函数中的内容被输出了两次,即每次都会获取一个新的bean的实例。

--------------------------------------------------------------------------------------------------------------------------------------------------------

接下来的几种模式的配置,由于博主目前还没有使用到,这里我们摘自官方文档,仅作概念上的介绍。对应的实例请读者们暂时自行完成。后续有对应的实例,博主也将及时更新,敬请期待!

--------------------------------------------------------------------------------------------------------------------------------------------------------

三,request作用域:在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext 情形下有效。

配置方法:

在web.xml中增加如下内容:

<web-app>    ...    <listener>        <listener-class>            org.springframework.web.context.request.RequestContextListener        </listener-class>    </listener>    ...</web-app>

<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>

--------------------------------------------------------------------------------------------------------------------------------------------------------

四,session作用域:在一个HTTP Session 中,一个bean定义对应一个实例。该作用域仅在基于web的SpringApplicationContext 情形下有效。

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

--------------------------------------------------------------------------------------------------------------------------------------------------------五,global 作用域:在一个全局的HTTP Session 中,一个bean定义对应一个实例。只是其用于portlet环境的web应用。如果在非portlet环境将视为session作用域。

<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>

--------------------------------------------------------------------------------------------------------------------------------------------------------

六,application作用域:同一个application共享一个bean。

<bean id="appPreferences" class="com.foo.AppPreferences" scope="application"/>

--------------------------------------------------------------------------------------------------------------------------------------------------------

至此,白话Spring(基础篇)---bean的作用域结束


参考资料:

Spring官网:http://docs.spring.io/spring/docs/4.3.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-factory-scopes-other


1 0
原创粉丝点击