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的区别。

添加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 23954271
p2's identityHashCode is 23954271
    <bean class="cn.outofmemory.spring.Person" scope="prototype">
<property name="id" value="1"/>
<property name="name" value="Jim"/>
</bean>

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

p1's identityHashCode is 23954271
p2'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 33212498
p2's identityHashCode is 24480977

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

原文地址 : http://outofmemory.cn/java/spring/spring-bean-scope

spring中的bean的属性scope的更多相关文章

  1. spring(四):spring中给bean的属性赋值

    spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...

  2. 1.2(Spring学习笔记)Spring中的Bean

    一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...

  3. 第2章 Spring中的Bean

    2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...

  4. Spring 中的bean 是线程安全的吗?

    结论: 不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但是具体还是要结合具体sco ...

  5. 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  6. 【Spring】Spring中的Bean - 4、Bean的生命周期

    Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...

  7. 【Spring】Spring中的Bean - 3、Bean的作用域

    Bean的作用域 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 通过Spring容器创建一个Bean的实例时,不仅可以完成 ...

  8. Spring 中的 bean线程安全性分析

    首先:Spring 中的 bean不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但 ...

  9. 传统javabean与spring中的bean的区别

    javabean已经没人用了 springbean可以说是javabean的发展, 但已经完全不是一回事儿了 用处不同:传统javabean更多地作为值传递参数,而spring中的bean用处几乎无处 ...

随机推荐

  1. ubuntu 14.04 安装 Apache Thrift 0.10

    1.到官网下载源码压缩文件 https://thrift.apache.org/download 2.安装依赖软件,可以参考 https://thrift.apache.org/docs/instal ...

  2. js分钟数转天-时-分

    //js格式化分钟转为天.时.分 function formatMinutes(minutes) { )); ? Math.floor((minutes - day * ) / ) : Math.fl ...

  3. liunx poi excel下载内容乱码本地tomcat正常

    结论:在jsp中加上out.clear即可(前提保证生成的excel在服务器上是正确的,只是浏览器传输才出现乱码). dowload.jsp完整代码 <%@ page language=&quo ...

  4. java 常用第3方工具

    https://www.cnblogs.com/chenpi/p/5608628.html#_label4

  5. Mysql索引,有哪几种索引,什么时候该(不该)建索引;SQL怎么进行优化以及SQL关键字的执行顺序

    索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 1.按照索引列值的唯一性,索引可分为唯一索引和非唯一索引 非唯一索引:B树索引 crea ...

  6. Centos6 下安装Nginx+Mysql+PHP

    安装nginx https://segmentfault.com/a/1190000007928556 添加源 $ wget http://nginx.org/packages/centos/6/no ...

  7. SAFESEH 映像是不安全的

    1.打开该项目的“属性页”对话框 2.然后单击“链接器”--“命令行”,出现如下界面 3.将 /SAFESEH:NO 复制到“其它选项(D)”框中,然后点击应用 返回VS2013,重新编译运行程序即可 ...

  8. C# 如何利用反射,将字符串转化为类名并调用类中方法

    首先,先随便创建一个测试类 <span style="font-family:Microsoft YaHei;font-size:18px;">public class ...

  9. js 编写一个神奇的四则运算

    写一个算法,有时候可以用简单的方法就可以写出来,但是只能针对特定的环境,如果要能够适应不同的环境,就需要对算法进行优化,在优化的过程中,你会觉得非常神奇,下面来看一个简单的四则运算的算法编写方式: 1 ...

  10. 切换用户身份su与sudo

    普通用户切换到root用户的方式有:su和sudo. 1,su - (su为switch user,即切换用户的简写) 格式:su -l USERNAME(-l为login,即登陆的简写) -l可以将 ...