详解Spring中bean的作用域

38 篇文章 1 订阅
20 篇文章 0 订阅

Spring中bean的作用域用scope来表示。scope的值有两个:singleton为单粒,即Spring IoC容器只会创建该bean的唯一一个实例,这也是默认的。该实例就会一直放在缓存里供大家使用。

AD:


    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. "1.0" encoding="UTF-8"?>    
    2.     
    3. <beans xmlns=< span="">"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.     <bean name=< span="">"bean1" class="com.cos.bean110317.Bean1" scope="singleton">    
    14.     

      当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. }    
    • 0
      点赞
    • 2
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值