spring中bean的scope属性

来源:互联网 发布:在家兼职淘宝客服58 编辑:程序博客网 时间:2024/05/19 21:40

spring中bean的scope属性,有如下5种类型:

  1. singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
  2. prototype表示每次获得bean都会生成一个新的对象
  3. request表示在一次http请求内有效(只适用于web应用)
  4. session表示在一个用户会话内有效(只适用于web应用)
  5. globalSession表示在全局会话内有效(只适用于web应用)

在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。

下面我们用一个示例来说明singleton和prototype两种scope的区别。

新建一个maven项目添加spring相关的maven依赖,如果需要请参照:http://outofmemory.cn/java/spring/spring-hello-world。

添加java类Person,Person的内容如下:

package cn.outofmemory.spring;public class Person {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

Person类是一个POJO类,我们不需要他做任何事情,只是为了证明prototype和singleton两种scope的异同之处。

我们还需要一个App类,他的代码如下:

package cn.outofmemory.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App {    public static void main( String[] args )    {        ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");        Person p1 = appContext.getBean(Person.class);        System.out.println("p1's identityHashCode is " + System.identityHashCode(p1));                Person p2 = appContext.getBean(Person.class);        System.out.println("p2's identityHashCode is " + System.identityHashCode(p2));            }}

在App类中的main方法中,首先我们初始化了ApplicationContext的实例,然后分别获得两次Person bean的实例p1,p2并分别输出其identityHashCode,如果两个对象是同一个对象,那么他们的identityHashCode应该是一致的。

添加source folder:src/main/conf,并新建spring配置文件spring.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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean class="cn.outofmemory.spring.Person"><property name="id" value="1"/><property name="name" value="Jim"/></bean></beans>

在此配置文件中我们定义了Person bean,没有指定其scope,这时候bean的scope为默认的singleton,也就是单例,此时App类的输出应该是两个相同的identityHashcode,我们运行程序看输出的结果:

p1's identityHashCode is 23954271p2's identityHashCode is 23954271

可以看到p1和p2是完全相同的,我们再修改spring.xml配置文件,将Person bean的scope修改为prototype:

<bean class="cn.outofmemory.spring.Person" scope="prototype"><property name="id" value="1"/><property name="name" value="Jim"/></bean>

再次运行程序,输出结果如下:

p1's identityHashCode is 23954271p2's identityHashCode is 13359324

可以看到p1和p2是两个不同的值,这说明scope是prototype的情况下,同一个bean定义会返回不同的对象。

我们也可以通过Scope注解来指定java bean的scope,我们给Person类添加如下注解:

import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component@Scope("prototype")public class Person {....省略}

@Component注解告诉spring,要加载此类,Scope注解bean的scope是prototype。

我们修改spring.xml配置文件,使用context:component-scan节点,让spring通过注解加载bean。

<?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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="cn.outofmemory.spring"></context:component-scan></beans>

再次运行程序,将输出:

p1's identityHashCode is 33212498p2's identityHashCode is 24480977

 可以看到使用Scope注解指定prototype scope后,两个Person对象是两个不同的对象。

0 0
原创粉丝点击