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定义相匹配,则 ...
随机推荐
- 教你如何更改xshell中的转发规则
使用不同的类型转发,与之对应的端口,所以如果想要使用不同类型的转发就要更改端口使其与之一一对应.本集xshell专栏文章将为大家讲解如何更改转发规则. 更改转发规则操作如下: 1.打开会话对话框. 2 ...
- Android面试收集录 Android系统的资源+其他
1.Android应用程序的资源是如何存储的,如何使用? res文件夹或者assets文件夹 res目录中的资源在R类中生成一个int变量,然后再布局文件中可以直接使用,在代码中,要getResour ...
- 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(三):设置上传文件夹权限(这里测试用完全共享)
基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django 基于Ubuntu Server 16.04 LTS版本安装和部署Djan ...
- [Hbase]hbase命令行基本操作
-进入hbase shell hbase shell - 帮助help help - 查看hbase versionversion - 查看hbase 状态 status - 创建表create 't ...
- java Spring boot使用spring反射
spring 反射 当你配置各种各样的bean时,是以配置文件的形式配置的,你需要用到哪些bean就配哪些,spring容器就会根据你的需求去动态加载,你的程序就能健壮地运行. 1.可以通过类名去实例 ...
- Asp.NET Core2.0与 EF的ABP框架入门视频教程
https://ke.qq.com/course/287301?from=qqchat&ADUIN=1187219916&ADSESSION=1522716499&ADTAG= ...
- OpenCV入门:(二:加载,显示,修改以及保存图片)
目标: 1.从图片文件打开图片(imread) 2.显示图片(namedWindow和imshow) 3.转换当前图片为灰色图片(cvtColor) 4.保存图片(imwrite) 代码: #incl ...
- 树莓派配置 USB 无线网卡
树莓派配置 USB 无线网卡来上网的过程. 本人使用的USB无线网卡型号:EP-N8508GS(树莓派专用型号) 一.检查 USB 无线网卡是否已经正确识别 将无线 USB 网卡插入树莓派后启动树莓派 ...
- Java项目启动时候报Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 解决办法
今天在发布Java项目的时候又遇到 Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At leas ...
- sql 条件插入
原普通插入语句: insert into seriestable_upload values(New.SeriesID,0); 加条件后:(当不存在该条数据插入) insert into custom ...