Spring中bean的作用域(scope),sington与prototype

在spring2.0之前bean只有2种作用域即:singleton(单例)、prototype,Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean,下面就让我们来看看singleton与prototype在spring的作用域(scope)中到底有什么区别

Java代码  收藏代码
  1. package spring.scope;  
  2.   
  3. public class Person {  
  4.   
  5.     private int id;  
  6.     private String name;  
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19. }  

 定义一个普通的Person类

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--  
  4.   - Application context definition for JPetStore's business layer.  
  5.   - Contains bean references to the transaction manager and to the DAOs in  
  6.   - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").  
  7.   -->  
  8. <beans xmlns="http://www.springframework.org/schema/beans"  
  9.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  10.          xmlns:aop="http://www.springframework.org/schema/aop"  
  11.          xmlns:tx="http://www.springframework.org/schema/tx"  
  12.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  13.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  14.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  15.   
  16. <!-- singleton prototype ||||session   request   global session 三种专用于Web应用程序上下文的Bean  
  17.         <bean id="bean1" class="spring.scope.Person" scope="prototype"/>  
  18.  -->  
  19.         <bean id="bean1" class="spring.scope.Person" scope="singleton"></bean>  
  20.           
  21.           
  22. </beans>  

 编写配置文件,首先将spring的scope定义为singleton类型

Java代码  收藏代码
  1. package spring.scope;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import junit.framework.TestCase;  
  7.   
  8. public class ScopeTest extends TestCase {  
  9.   
  10.     private BeanFactory beanFactory;  
  11.       
  12.     @Override  
  13.     protected void setUp() throws Exception {  
  14.         beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");  
  15.     }  
  16.       
  17.     public void testScope1() {  
  18.         Person p1= (Person)beanFactory.getBean("bean1");  
  19.         Person p2= (Person)beanFactory.getBean("bean1");  
  20.         if(p1 == p2) {  
  21.             System.out.println("对象相等");  
  22.         } else {  
  23.             System.out.println("对象不相等");  
  24.         }  
  25.     }  
  26. }  

 由于上面配置的是singleton,当然打印的是对象相等,如果配置的是prototype那么打印的就将是对象不相等了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值