【spring】--Bean做用域

  Spring框架功能强大,其中一个经常使用的功能是使用IOC容器来管理Bean,在建立Bean的过程当中,主要有5中做用域,下面就来介绍一下每个做用域的特色和做用。html

  一、Singleton:设置为该模式的Bean,容器初始化时就建立一个Bean实例,并且该实例只存在一份。测试以下:java

   建立SingleScope类:    web

 

public class SingleScope {
    
    //建立空构造函数,用于测试IOC容器初始化时就建立该实例bean
    public SingleScope(){        
        System.out.println("Constructor go ---->");
    }
}

   

 

  配置spring的配置文件ApplicationContext-scope.xml:   spring

 

<?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.0.xsd
           ">
    <!--配置bean,设置为singleton模式-->
    <bean id="singleScope" class="com.wpb.scope.SingleScope" scope="singleton"/>

</beans>

 

 

  建立测试类:    设计模式

 

public class ScopeTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-scope.xml");
        
        SingleScope singleScope= (SingleScope) context.getBean("singleScope");
        SingleScope singleScope1= (SingleScope) context.getBean("singleScope");
        System.out.println(singleScope==singleScope1);
    }
}

 

 

  结果以下:session

   


   须要注意的是:spring中bean的singleton模式与设计模式中的单例模式是两个不一样的概念,他们是有相同的特色,只实例一次。可是设计模式中的单例模式指的是ClassLoader加载该类时,只实例化一次。而bean的singleton模式指的是Spring IOC容器中只存在一份该bean。框架


 二、Prototype:能够存在多个bean实例,容器初始化时不会建立bean实例,使用时才会建立。来看一下测试代码:函数

  建立PrototypeScope类:   测试

 

/**
 * Created by 王朋波 on 16/07/2017.
 */
public class PrototypeScope {
    public PrototypeScope(){
        System.out.println("PrototypeScope constructor go --->");
    }
}

 

 

  在spring的配置文件中添加以下配置:   spa

 

<!--配置bean,设置为Prototype模式 -->
    <bean id="prototypeScope" class="com.wpb.scope.PrototypeScope" scope="prototype"/>

 

 

   测试代码:    

 

 //当使用bean时,才建立实例。执行完下述获取实例语句,PrototypeScope中的构造函数才会执行
        PrototypeScope prototypeScope= (PrototypeScope) context.getBean("prototypeScope");
        PrototypeScope prototypeScope1= (PrototypeScope) context.getBean("prototypeScope");
        System.out.println(prototypeScope==prototypeScope1);

 

 

   运行结果:

    


   注意的地方:Prototype bean被spring容器配置、装载、实例化以后,其生命周期将由本身负责,也就是spring容器再也不管理它的生命周期,交给了客户端进行管理。


 比较经常使用的是singleton和Prototype,下面三种不经常使用,简单了解一下基本概念和配置方式。

  三、request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不一样的Bean实例。仅在基于web的Spring ApplicationContext情形下有效。

  四、session:对于每次HTTP Session,使用session定义的Bean豆浆产生一个新实例。仅在基于web的Spring ApplicationContext情形下有效。 

  五、每一个全局的HTTP Session,使用session定义的Bean都将产生一个新实例。典型状况下,仅在使用portlet context的时候有效。仅在基于web的Spring ApplicationContext情形下有效。

  上述三种方式除了配置spring的配置文件以外,还须要在web.xml文件增长一个监听设置,以下: 

 

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

 

 

  小结:

   bean的做用域中,须要重点掌握Singleton和Prototype两种方式,另外还须要与Struts2中配置Action时,也有这两种模式做对比,明白各自的含义以及默认使用。

相关文章
相关标签/搜索