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定义相匹配,则 ...
随机推荐
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- centos7下安装elasticSearch错误总结(单节点模式)
1.首先确定你安装了jdk,版本需要1.8以上 2.上传elasticsearchjar包,只需配置一个文件即可 修改配置文件config/elasticsearch.yml network.h ...
- Linux/CentOS防CC攻击脚本
#!/bin/sh cd /var/log/httpd/ cat access_log|awk > a cp /dev/null access_log cp /dev/null error_lo ...
- 【数据库】 SQL SERVER 2014 实用新特性
[数据库] SQL SERVER 2014 实用新特性 官方链接 一. 内存优化表 大幅提高数据库性能,不过目前没有窗口化设计只能写语句 二. 索引增强
- 链接程序的时候遇到问题:fatal error LNK1104: cannot open file 'rctrl-d.lib'
1.lib库文件没有添加到工程中(工程里面根本就没有这个文件) 2.
- sqlserver 找出字符第N次出现的位置
[1编写函数]CREATE FUNCTION IndexOf(@str VARCHAR(500),@value VARCHAR(50),@posIndex INT)RETURNS int AS BEG ...
- webmagic 二次开发爬虫 爬取网站图片
webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫. webmagic介绍 编写一个简单的爬虫 webmagic的使用文档:http://w ...
- poj1789 Truck History最小生成树
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- springmvc项目搭建四-基于前端框架完善页面的数据显示
上一篇把前端框架先放上去了,现在开始前后端进行交互,对数据进行显示. 效果如图所示...中间经历了数据显示不上去的问题,是对于spring的注解了解不够,问题及其解决可以参照上一篇问题处理... 目前 ...
- springboot ueditor 使用心得
1.将ueditor引入项目中会发现,图片不能上传,返回值意思是因配置文件错误,导致图片无法上传 默认情况是使用jsp初始配置文件,这就需要项目支持jsp解析 在maven中引入 <!--添加对 ...