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. python中的swapcase

    swapcase()将字符串中的字母小写变大写.大写变小写,举个例子: 1 a = "hELLO wORLD" 2 a1 = a.swapcase() 3 print(a1) 输出 ...

  2. jieba安装

    执行“pip install jieba”后忽略此条提示" You are using pip version 9.0.3, however version 10.0.1 is availa ...

  3. JS 函数(arguments、箭头函数、bind)

    参数 函数内部可用的 arguments 对象来访问函数的实参 注意 在函数递归调用的时候(在某一刻同一个函数运行了多次,也就是有多套实参),那么 arguments 属性的值是最近一次该函数调用时传 ...

  4. 线程池之 newScheduledThreadPool中scheduleAtFixedRate(四个参数)

    转自:https://blog.csdn.net/weixin_35756522/article/details/81707276 说明:在处理消费数据的时候,统计tps,需要用一个线程监控来获得tp ...

  5. 轻量级Java持久化框架,Hibernate完美助手,Minidao 1.6.2版本发布

    Minidao 1.6.2 版本发布,轻量级Java持久化框架(Hibernate完美助手) Minidao产生初衷? 采用Hibernate的J2EE项目都有一个痛病,针对复杂业务SQL,hiber ...

  6. 关于 C++ 默认构造函数 的几个误区 转载

    https://blog.csdn.net/ccrazyman/article/details/8138425

  7. Delphi 泛型详解

    http://www.cnblogs.com/jxgxy/category/216671.html

  8. zabbix简易安装指南

    1.php版本为5.5.37 mysql版本为5.5 nginx使用的是tengine2.2.1 2.系统为centos 7.2 最小化安装 3.使用阿里云的epel扩展源 4.首先安装依赖 yum ...

  9. Delphi Class of 类引用

    Delphi Class of 类引用也就是类的类型,也可说是指向类的指针 Type TControlCls = Class of TControl;function CreateComponent( ...

  10. C++ 动态创建按钮及 按钮的消息响应

    动态创建的按钮 都会在消息 OnCommand 中得到处理,无论是什么消息,都会处理的 1\创建按钮 CButton* btn = new CButton(); btn->Create(_T(, ...