Spring学习-- Bean 的作用域
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... |
在 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... |
调用两次 getBean() , 每一次都会实例化一次 , 创建两个新的实例。
Spring学习-- Bean 的作用域的更多相关文章
- 详解Spring中Bean的作用域与生命周期
摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring中Bean的作用域
作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...
- Spring之Bean的作用域与生命周期
在前面博客中提到容器启动获得BeanDefinition对象中有一个scope 属性.该属性控制着bean对象的作用域.本章节介绍Bean的作用域及生命周期,了解bean是怎么来的又怎么没的. 一.B ...
- Spring中Bean的作用域、生命周期
Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...
- Spring基础—— Bean 的作用域
一.在 Spring Config 文件中,在 <bean> 元素的 scope 属性里设置 Bean 的作用域.默认为 singleton ,单例的. 二.在不引入 spring-web ...
- spring之bean的作用域scope的值的详解
今天研究了一下scope的作用域.默认是单例模式,即 scope="singleton".另外scope还有prototype.request.session.global ses ...
- Spring、Bean 的作用域
Singleton作用域(默认) 当一个bean的作用域为singleton,那么Spring Ioc容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...
随机推荐
- Git使用之二:下载远程代码到本地指定文件夹
一.前期工作: 1.准备好本地的文件夹 2.如果后期需要继续以该文件夹进行同步的,则需要配置该文件夹,方法请参考之前的 Git使用之一:创建仓储和提交文件 二.用clone(克隆方式下载) 在本地下 ...
- C++语言入门知识点(详细版)【持续更新每周三更】,小舒舒戳这里!!!
时间过得好快啊,LITTLESUN已经在这块新地图摸打滚爬了一个多月了.前一段时间出了点小意外一直没能更新博客,昨天被小舒舒催更了(惭愧惭愧)便准备着手来一篇回忆录回首一下这一个月走过的风风雨雨,也希 ...
- C#获取网络图片
简单获取图片 string url = zhi_txt.Text;//图片地址 string dizhi = lujing.Text;//图片下载后保存路径及图片名称要写在一块 WebClient w ...
- CSS实现简易的轮播图
<html> <head> <meta charset="UTF-8"> <title></title> <sty ...
- python------- IO 模型
IO模型介绍 ...
- go 语言模拟百度登录
1.参考网上Python的例子自己写了一个go语言的.这个仅供学习技术参考,为了方便有部分参数直接phantomjs执行js获取,代码基本都有注释,测试打印没有删除,还请见谅! 2.本文参考http: ...
- 信号处理是Unix和LInux系统为了响应某些状况而产生的事件
信号处理是Unix和LInux系统为了响应某些状况而产生的事件,通常内核产生信号,进程收到信号后采取相应的动作. 例如当我们想强制结束一个程序的时候,我们通常会给它发送一个信号,然后该进程会捕捉到信号 ...
- 数据结构14——AC自动机
一.相关介绍 知识要求 字典树Trie KMP算法 AC自动机 多模式串的字符匹配算法(KMP是单模式串的字符匹配算法) 单模式串问题&多模式串问题 单模就是给你一个模式串,问你这个模式串是否 ...
- NO1——线段树
/* 数组存储 */ /* 预处理 */ #include <iostream> #include <cstdio> #include <algorithm> #i ...
- 打包成exe程序
https://blog.csdn.net/harvic880925/article/details/27675073 当然针对这种exe文件,也可以解压出来得到具体的debug文件夹下的内容.