亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

spring bean的scope屬性

標簽:
Java

spring中bean的scope属性,有如下5种类型:

  1. singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例

  2. prototype表示每次获得bean都会生成一个新的对象

  3. request表示在一次http请求内有效(只适用于web应用)

  4. session表示在一个用户会话内有效(只适用于web应用)

  5. globalSession表示在全局会话内有效(只适用于web应用)

在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。

下面我们用一个示例来说明singleton和prototype两种scope的区别。

新建一个maven项目添加spring相关的maven依赖,如果需要请参照:

在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

可以看到p1和p2是完全相同的,我们再修改spring.xml配置文件,将Person bean的scope修改为prototype:

 <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对象是两个不同的对象。

本文源码下载:spring-bean-scope.zip

原文链接:http://outofmemory.cn/java/spring/spring-bean-scope

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消