详解Spring中bean的作用域
创始人
2024-07-23 11:41:21
0

Spring中bean的作用域

Spring中bean的作用域用scope来表示。

scope的值有两个:

singleton为单粒,即Spring IoC容器只会创建该bean的***一个实例,这也是默认的。该实例就会一直放在缓存里供大家使用。

prototype为原型,即每一次请求都会产生一个新的bean实例。

以实例说明

Bean

Java代码

  1. package com.cos.bean110317;     
  2.     
  3. public class Bean1 {     
  4.     
  5.     private String name;     
  6.     
  7.     public String getName() {     
  8.         return name;     
  9.     }     
  10.     
  11.     public void setName(String name) {     
  12.         this.name = name;     
  13.     }     
  14. }    

 

applicationContext_5.xml

Xml代码

 

  1.     
  2.     
  3. "http://www.springframework.org/schema/beans"    
  4.     xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms"    
  5.     xmlns:context="http://www.springframework.org/schema/context"    
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  7.     xsi:schemaLocation="     
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  9. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd     
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  11. http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">    
  12.     
  13.     "bean1" class="com.cos.bean110317.Bean1" scope="singleton">    
  14.         
  15.         "name" value="zhangsan"/>    
  16.         
  17.    

 

 

 

 

当scope不写的时候默认为singleton

当scope为singleton的时候,输出结果bean11 == bean12

当scope为prototype的时候,输出结果bean11 != bean12

测试类

Java代码

 

 

  1. package com.cos.bean110317;     
  2.     
  3. import org.springframework.beans.factory.BeanFactory;     
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;     
  5.     
  6. public class Test {     
  7.     
  8.     public static void main(String[] args) {     
  9.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext_5.xml");     
  10.         Bean1 bean11 = (Bean1) factory.getBean("bean1");     
  11.         Bean1 bean12 = (Bean1) factory.getBean("bean1");     
  12.         if(bean11 == bean12){     
  13.             System.out.println("bean11 == bean12");     
  14.         }else{     
  15.             System.out.println("bean11 != bean12");     
  16.         }     
  17.     }     
  18. }    

【编辑推荐】

    Struts VS Spring两种MVC框架比较

    Spring MVC框架的高级配置

    Spring MVC框架的高级配置(上)

    Spring MVC框架的高级配置(下)

    设计REST风格的Java MVC框架:WebWind

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...