详解Spring中bean的作用域

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

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. "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. }    

【编辑推荐】

责任编辑:金贺 来源: JavaEye博客
相关推荐

2023-09-05 08:23:56

SpringScope方法

2021-07-05 08:43:46

Spring Beanscope作用域

2024-01-05 08:38:20

SpringBeanScope

2023-06-29 08:32:41

Bean作用域

2009-06-17 17:04:37

BeanFactorySpring

2021-03-08 08:40:25

Spring Bean 创建单例对象

2024-02-23 10:33:34

SpringBean容器

2016-12-19 11:10:32

JavaScript变量作用域

2022-08-31 07:04:50

Bean作用域

2021-06-02 07:02:42

js作用域函数

2011-05-12 18:26:08

Javascript作用域

2020-09-26 07:19:46

Java

2009-06-17 17:20:14

BeanFactorySpring

2021-07-01 10:45:18

Bean对象作用域

2024-03-14 11:27:16

C++变量编程

2009-06-01 11:16:48

PHP网站开发变量作用域

2010-09-29 15:02:23

DHCP作用域

2022-05-27 08:25:55

容器Spring

2014-11-06 09:49:22

CloudFoundrPaaS

2014-11-06 10:46:48

CloudFoundr
点赞
收藏

51CTO技术栈公众号