Bean 的作用域:

  • 在 Spring 中 , 可以在 <bean> 元素的 scope 属性里设置 bean 的作用域。
  • 默认情况下 , Spring 只为每个在 IOC 容器里声明的 bean 创建唯一一个实例 , 整个 IOC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 bean 引用都将返回这个唯一的 bean 实例 , 该作用域被称为 singleton , 他是所有 bean 的默认作用域 , 属于单例对象。

类别

说明

Singleton

在 Spring IOC 容器中仅存在一个 bean 实例 , bean 以单例的方式存在

Prototype

每调用一次 getBean() 都会生成一个新的实例

Request

每次 HTTP 请求都会创建一个新的 bean , 该作用域仅适用于 WebApplicationContext 环境

Session

同一个 HTTP Session 共享一个 bean , 不同的 HTTP Session 使用不同的 bean , 该作用域仅适用于 WebApplicationContext 环境

Singleton:

 package com.itdjx.spring.beans.scope;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-02 8:34
*/
public class Main { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("scope_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="person" class="com.itdjx.spring.beans.scope.Person">
<property name="name" value="华崽儿"/>
<property name="age" value="27"/>
</bean> </beans>
 package com.itdjx.spring.beans.scope;

 /**
* @author Wáng Chéng Dá
* @create 2017-03-02 8:33
*/
public class Person { private String name; private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Person() {
System.out.println("I am scope.Person Constructor...");
}
}

控制台输出:

I am scope.Person Constructor...
 package com.itdjx.spring.beans.scope;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-02 8:34
*/
public class Main { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml");
Person person = (Person) app.getBean("person"); Person person1 = (Person) app.getBean("person");
System.out.println("person==person1: " + (person == person1)); }
}

控制台输出:

I am scope.Person Constructor...
person==person: true

在 IOC容器初始化时就已经将 bean 实例化。当新创建对象时 , 没有调用构造函数 , 两个实例 == 比较时是 true , 说明这只在 IOC 容器初始化的时候创建了一次 bean 的实例。

Prototype:

 <?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="person" class="com.itdjx.spring.beans.scope.Person" scope="prototype">
<property name="name" value="华崽儿"/>
<property name="age" value="27"/>
</bean> </beans>
package com.itdjx.spring.beans.scope;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-02 8:34
*/
public class Main { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml"); }
}

控制台输出:

控制台没有输出
 package com.itdjx.spring.beans.scope;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-02 8:34
*/
public class Main { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("scope_beans.xml");
Person person = (Person) app.getBean("person"); Person person1 = (Person) app.getBean("person"); System.out.println("person==person1: " + (person == person1)); }
}

控制台输出:

I am scope.Person Constructor...
I am scope.Person Constructor...
person==person1: false

调用两次 getBean() ,  每一次都会实例化一次 , 创建两个新的实例。

Spring学习-- Bean 的作用域的更多相关文章

  1. 详解Spring中Bean的作用域与生命周期

    摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...

  2. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  3. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring中Bean的作用域

    作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...

  5. Spring之Bean的作用域与生命周期

    在前面博客中提到容器启动获得BeanDefinition对象中有一个scope 属性.该属性控制着bean对象的作用域.本章节介绍Bean的作用域及生命周期,了解bean是怎么来的又怎么没的. 一.B ...

  6. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  7. Spring基础—— Bean 的作用域

    一.在 Spring Config 文件中,在 <bean> 元素的 scope 属性里设置 Bean 的作用域.默认为 singleton ,单例的. 二.在不引入 spring-web ...

  8. spring之bean的作用域scope的值的详解

    今天研究了一下scope的作用域.默认是单例模式,即 scope="singleton".另外scope还有prototype.request.session.global ses ...

  9. Spring、Bean 的作用域

    Singleton作用域(默认) 当一个bean的作用域为singleton,那么Spring Ioc容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...

随机推荐

  1. [拉格朗日反演][FFT][NTT][多项式大全]详解

    1.多项式的两种表示法 1.系数表示法 我们最常用的多项式表示法就是系数表示法,一个次数界为\(n\)的多项式\(S(x)\)可以用一个向量\(s=(s_0,s_1,s_2,\cdots,s_n-1) ...

  2. git的关于测试的相关的内容

    今天,我们来讲一下git的分支的一些内容,在以前的时候,我一直都以为,对于一个项目,这个时候,我们把这个项目叫做项目a项目,这个a项目有master,staging,以及我自己的分支xxx,当我想上测 ...

  3. ExtJs工具篇(1)——在Aptana3中安装ExtJS 代码提示插件

    首先得下载Aptana 这个软件,我下载的是Aptana3这个版本.下载后,在"帮助"菜单中选择"安装新软件",弹出下面的对话框: 我们需要安装一个叫做&quo ...

  4. Get Error when restoring database in Sql Server 2008 R2

         When I restored a database I got an error: "The backup set holds a backup of a database ot ...

  5. es2017中的async和await要点

    1. async和await最关键的用途是以同步的写法实现了异步调用,是对Generator异步方法的简化和改进.使用Generator实现异步的缺点如下: 得有一个任务执行器来自动调用next() ...

  6. jackson 处理空值

    @JsonInclude(value=Include.NON_NULL) public class ResultBean 这样在返回数据的时候, { "code": "s ...

  7. 【转】ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了

    原文链接:https://www.cnblogs.com/yilezhu/p/9241261.html 引言 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必 ...

  8. 面向对象 公有私有 property classmethod staticmethod

    接口类(抽象类)--------就是一种规范 面向对象的私有与公有 对于每一个类的成员而言都有两种形式: 公有成员,在任何地方都能访问 私有成员,只有在类的内部才能方法 私有成员和公有成员的访问限制不 ...

  9. 参加2018之江杯全球人工智能大赛 :视频识别&问答(四)

    很遗憾没有在规定的时间点(2018-9-25 12:00:00)完成所有的功能并上传数据,只做到写了模型代码并只跑了一轮迭代,现将代码部分贴出. import keras from keras.lay ...

  10. lintcode-74-第一个错误的代码版本

    74-第一个错误的代码版本 代码库的版本号是从 1 到 n 的整数.某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错.请找出第一个错误的版本号. 你可以通过 isBad ...