spring中的bean的属性scope
spring中bean的scope属性,有如下5种类型:
- singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
- prototype表示每次获得bean都会生成一个新的对象
- request表示在一次http请求内有效(只适用于web应用)
- session表示在一个用户会话内有效(只适用于web应用)
- 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的更多相关文章
- spring(四):spring中给bean的属性赋值
spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...
- 1.2(Spring学习笔记)Spring中的Bean
一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...
- 第2章 Spring中的Bean
2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...
- Spring 中的bean 是线程安全的吗?
结论: 不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但是具体还是要结合具体sco ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- 【Spring】Spring中的Bean - 4、Bean的生命周期
Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...
- 【Spring】Spring中的Bean - 3、Bean的作用域
Bean的作用域 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 通过Spring容器创建一个Bean的实例时,不仅可以完成 ...
- Spring 中的 bean线程安全性分析
首先:Spring 中的 bean不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但 ...
- 传统javabean与spring中的bean的区别
javabean已经没人用了 springbean可以说是javabean的发展, 但已经完全不是一回事儿了 用处不同:传统javabean更多地作为值传递参数,而spring中的bean用处几乎无处 ...
随机推荐
- 《算法》第四章部分程序 part 11
▶ 书中第四章部分程序,包括在加上自己补充的代码,二分图的判定和染色 ● 二分图 1 //+------------------------------------------------------ ...
- hive 索引
hive 有限的支持索引,不支持主键外键,可以对表添加索引,也可以为某个分区添加索引.维护索引也要额外的存储空间和计算资源. 创建索引需要指定索引处理器 如 as 'org.apache.hadoop ...
- nodejs操作monggodb数据库封装
var MongoClient=require('mongodb').MongoClient; var DbUrl='mongodb://localhost:27017/productmanage'; ...
- CRC16-CCITT C语言代码
代码如下,使用空间换时间的方法 #define CRC16_CCITT_SEED 0xFFFF // 该位称为预置值,使用人工算法(长除法)时 需要将除数多项式先与该与职位 异或 ,才能得到最后的除数 ...
- PhotoShop阵列功能
阵列有两种,如下.但是PS没有阵列这一工具,一定要用ps的话,可以参照以下两条: 1:方形阵列 先按CTRL+ALT+T 会出现一个自由变换选取 但是这个是多重复制的选取只要一动就能复制了 确定 然后 ...
- 爬虫--requests模块高级(代理和cookie操作)
代理和cookie操作 一.基于requests模块的cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests ...
- SpringBoot读取application.properties文件内容
application.properties存储数据的方式是key-value. application.properties内容 userManager.userFile=data/user.pro ...
- Leetcode 题解 reverse List II
这个题确实太容易错了. 我已经做了2遍了,之前都是套用reverse List 1中的函数. 现在尝试用新方法,在一个函数里完成,结果又错了. 事实证明,永远不要想当然!!!白板编程真的是要求,你对每 ...
- SourceTree commit information window消失解决办法
https://answers.atlassian.com/questions/15282793/sourcetree-how-to-show-commit-information-panel 执行命 ...
- UNITY优化资料收集
U3D手册: Optimizing garbage collection in Unity games https://zhuanlan.zhihu.com/p/25306993 https://gi ...