spring管理bean作用域

在前面,将bean交给了Spring容器管理,在客户端只要调用getBean方法就可以从容器里获取bean实例,大家想想,每调用getBean方法,那么它从容器里获取的bean到底是同一个呢?还是不同?   怎么判断是否是同一个对象呢?可以这样做,代码如下:
SpringTest.java
Java代码
package junit.test;  
 
import org.junit.BeforeClass;  
import org.junit.Test;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
 
import cn.itcast.service.PersonService;  
 
public class SpringTest {  
 
    @BeforeClass 
    public static void setUpBeforeClass() throws Exception {  
    }  
 
    @Test 
    public void instanceSpring() {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
        PersonService personService1 = (PersonService) ctx.getBean("personService");  
        PersonService personService2 = (PersonService) ctx.getBean("personService");  
        System.out.println(personService1 == personService2);  
        // 怎么判断两个对象获取的是否是同一个? 很简单,比较引用是否相同就可以了  
    }  

package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringTest {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @Test
 public void instanceSpring() {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  PersonService personService1 = (PersonService) ctx.getBean("personService");
  PersonService personService2 = (PersonService) ctx.getBean("personService");
  System.out.println(personService1 == personService2);
  // 怎么判断两个对象获取的是否是同一个? 很简单,比较引用是否相同就可以了
 }
}
beans.xml
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
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean> 
</beans> 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean>
</beans>如果返回为true,代表他们引用的对象都是同一个;如果返回的是false,那么得到的就是两个不同的对象。
运行单元测试代码,输出结果为 true。 说明personService1和personService2这两个变量所引用的对象都是同一个,这就证实了:在默认情况下,bean交给Spring容器管理后,那么这个bean是一个单实例。
   那么我们能不能改变这种行为呢?假如现在我不想用单实例,我希望每调用一次getBean方法都获取一个新的实例,那该怎么做呢?  这就牵涉到Bean的作用域了


Bean的作用域
----------------------------------------------------------------
.singleton
在每个Spring IoC容器中一个bean定义只有一个对象实例。默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>

prototype    每次从容器获取bean都是新的对象。
request
session
global session


.request.session .global session 这三种是web应用才能用的。这里不是web应用,所以不能用。
上面那个单例模式指定的就是singleton作用范围。  如果没有指定作用域的话,那么默认就是singleton作用域范围。
当业务需求时每调用一次getBean方法,都要获取一个新的实例的话,这个时候就可以通过prototype这个属性值来指定。这个属性值代表:每调用一下getBean方法,都能从容器里获取到新的实例

SpringTest.java不变。
beans.xml如下:
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
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean> 
</beans> 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean>
</beans>运行单元测试代码,得出的是false,代表每调用一下getBean方法,它都会返回一个新的对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值