说说 Spring Bean 的作用域

配置文件中定义 Bean 时,我们不但可以配置 Bean 的属性值以及相互之间的依赖关系,还可以定义 Bean 的作用域 。作用域会对 Bean 的生命周期和创建方式产生影响 。

Bean 的作用域类型:

类型 说明
singleton 在 Spring 容器中仅存在一个 Bean 实例, Bean 以单例的形式存在。
prototype 每次从容器中调用 Bean 时,都会返回一个新的实例,即相当于执行 new XxxBean() 的实例化操作。
request 每次 http 请求都会创建一个新的 Bean , 仅用于 WebApplicationContext 环境。
session 同一个 http Session 共享一个 Bean ,不同的 http Session 使用不同的 Bean,仅用于 WebApplicationContext 环境。
globalSession 同一个全局 Session 共享一个 bean, 用于 Porlet, 仅用于 WebApplication 环境。

低版本的 Spring 中,仅支持两个 Bean 作用域(singleton 与 prototype),所以之前的配置为 singleton=true/false。Spring 为了向后兼容,依然支持这种配置方式。我们推荐采用新的配置方式 scope=<作用域类型>

1 singleton 作用域

Spring 以容器的方式,使得我们仅需配置,即可得到天然的单例模式。

一般情况下,无状态或者状态不可变的类适合使用单例模式来实现, 不过 Spring 利用 AOP 和 LocalThread 的能力,对非线程安全的变量(状态)进行了特殊处理,使的一些非线程安全的类(持有 Connection 的 DAO 类)变成了线程安全的类 。

因为 Spring 的超强能力,所以在实际应用中,大部分 Bean 都能以单例方式运行 ,这也是 bean 的默认作用域指定为 singleton 的原因 。

singleton 的 Bean 在同一个 Spring IoC 容器中只会一个实例。

配置:

<!-- singleton 作用域 -->
<bean id="author" class="net.deniro.spring4.bean.Author" scope="singleton"/>
<bean id="book1" class="net.deniro.spring4.bean.Book" p:author-ref="author"/>
<bean id="book2" class="net.deniro.spring4.bean.Book" p:author-ref="author"/>
<bean id="book3" class="net.deniro.spring4.bean.Book" p:author-ref="author"/>

单元测试:

System.out.println(((Book) context.getBean("book1")).getAuthor().hashCode());
System.out.println(((Book) context.getBean("book2")).getAuthor().hashCode());
System.out.println(((Book) context.getBean("book3")).getAuthor().hashCode());

输出结果:

813656972
813656972
813656972

这证明 Author 类在容器中是一个单例。

singleton 作用域

不仅在配置文件中注入的 author 引用的是同一个 author Bean,任何通过容器的 getBean("author") 返回的实例,引用的也是同一个 Bean。

默认情况下, Spring 的 ApplicationContext 容器在启动时,会自动实例化所有 singleton 的 Bean 并缓存在容器中 。 虽然启动时会多花费一些时间,但是有这些好处:
* 对 Bean 提前进行实例化操作会及早发现一些潜在的配置问题。
* Bean 以缓存的方式保存,当运行时调用该 Bean 时就无须再次实例化咯,因此提高运行效率 。

如果不希望在容器启动时提前实例化 singleton 的 Bean ,那么可以通过 lazy-init 属性进行控制:

<bean id="book1" class="net.deniro.spring4.bean.Book" p:author-ref="author" lazy-init="true"/>

如果某个 Bean 被其他需要提前实例化的 Bean 所引用,那么它即使配置了 lazy-init="true" ,也仍然会被提前实例化。

2 prototype 作用域

配置了 scope="prototype" 的 bean 为非单例作用域。

<bean id="author10" class="net.deniro.spring4.bean.Author" scope="prototype"/>
<bean id="book11" class="net.deniro.spring4.bean.Book" p:author-ref="author10"/>
<bean id="book12" class="net.deniro.spring4.bean.Book" p:author-ref="author10"/>
<bean id="book13" class="net.deniro.spring4.bean.Book" p:author-ref="author10"/>

单元测试:

System.out.println(((Book) context.getBean("book11")).getAuthor().hashCode());
System.out.println(((Book) context.getBean("book12")).getAuthor().hashCode());
System.out.println(((Book) context.getBean("book13")).getAuthor().hashCode());

输出结果:

2048425748
1863932867s
1373810119

这证明 Author 类在容器中是不同的实例。

prototype 作用域

在默认情况下, Spring 容器在启动时不实例化 prototype 的 bean ,此外, Spring 容器将 prototype 的 bean 交给调用者后,就不再负责管理它的生命周期咯。

3 与 Web 应用环境相关的作用域

如果使用 Spring 的 WebApplicationContext ,则可以使用另外 3 种 Bean 的作用域 (request、session 和 gloableSession)。

3.1 配置监听器

首先必须在 Web 容器中进行一些配置:

高版本的 Web 容器( Servlet 2.3 +)中,配置 http 请求监听器:

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

低版本的 Web 容器,需要配置 http 请求过滤器(RequestContextFilter)。

它与 ContextLoaderListener 的区别是:
* ContextLoaderListener 实现了 ServletContextListener 监听器接口, 它只负责监听 web 容器的启动和关闭事件 。
* RequestContextListener 实现了 ServletRequestListener 监听器接口,它监听 HTTP 请求事件, Web 服务器的每一次请求都会通知它 。

3.2 request 作用域

request 作用域的 Bean 对应一个 HTTP 请求和生命周期 。

假设:

<bean id="author" class="net.deniro.spring4.bean.Author" scope="request"/>

每次 HTTP 请求调用 author Bean 时, Spring 容器就会创建一个新的 author Bean ;请求处理完毕,就会销毁这个 Bean。

3.3 session 作用域

假设:

<bean id="author" class="net.deniro.spring4.bean.Author" scope="session"/>

author Bean 的作用于横跨整个 HTTP Session。Session 中的所有 HTTP 请求会共享同一个 author Bean. 只有当 HTTP Session 结束后,author 实例才会被销毁 。

3.4 globalSession 作用域

假设:

<bean id="author" class="net.deniro.spring4.bean.Author" scope="globalSession"/>

globalSession 的作用域类似于 session 作用域, 不过仅在 Portlet 的 Web 应用中使用 。 Portlet 定义了全局 Session,它被组成 Portlet Web 应用的所有子 Portlet 共享。如果不在 Portlet 的 Web 应用下,globalSession 等价于 session。

4 作用域依赖

假设我们需要在 非 web 作用域下的 Bean 中引用 web 作用域下的 Bean,那么这里我们就需要使用 Spring AOP 的功能。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       ">

    <!-- 非 web 作用域引用 web 作用域-->
    <bean id="author20" class="net.deniro.spring4.bean.Author" scope="request">
        <!-- 创建代理-->
        <aop:scoped-proxy/>
    </bean>
    <bean id="book20" class="net.deniro.spring4.bean.Book" p:author-ref="author20"/>

</beans>

首先引入了 AOP 文件头说明。

author Bean 的作用域是 request , 它被 singleton 作用域的 book Bean 所引用 。 为了使 book 能从 request 的作用域中获取 author 的引用, 这里使用了 Spring AOP 为 book Bean 声明了一个代理类。

当 book Bean 在 web 环境中调用 author Bean 时, Spring AOP 将启动动态代理机制智能判断 author Bean 处于哪一个 HTTP 请求线程中,并从对应的 HTTP 请求线程中回去对应的 author Bean。

request 作用域下的请求线程

Spring 通过动态代理技术,能够让单例的 book bean 引用到对应 HTTP 请求的 author Bean。

动态代理会判断当前的 book 位于哪一个线程中,然后根据这个线程找到对应的 HttpRequest,接着再从 HttpRequest 中获取对应的 author。根据容器的特性,一个 HTTP 请求对应一个独立的线程。

因为 Java 语言只能对接口实现自动代理,因此,Spring 是通过 CGLib 库来实现类的动态代理的,所以如果有需要,请在类路径中加入 CGLib 库。

猜你喜欢

转载自blog.csdn.net/deniro_li/article/details/80225878