[JAVA] 解读SpringBean的作用域

2376 0
王子 2022-11-8 16:59:58 | 显示全部楼层 |阅读模式
目录

    目前Spring Bean的作用域或者说范围主要有五种(1)被声明为singleton的bean(2)被声明为prototype的bean(3)使用注解定义 bean 的作用域(4)请求作用域(5)会话作用域(6)全局作用域(7)自定义作用域

在Spring中,bean作用域用于确定哪种类型的bean实例应该从Spring容器中返回给调用者。

目前Spring Bean的作用域或者说范围主要有五种

作用域描述
singleton在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,bean作用域范围的默认值。
prototype每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()。
request每次HTTP请求都会创建一个新的Bean,该作用域仅适用于web的Spring WebApplicationContext环境。
session同一个HTTP Session共享一个Bean,不同Session使用不同的Bean。该作用域仅适用于web的Spring WebApplicationContext环境。
application限定一个Bean的作用域为ServletContext的生命周期。该作用域仅适用于web的Spring WebApplicationContext环境。

(1)被声明为singletonbean

如果bean的作用域的属性被声明为singleton,那么Spring Ioc容器只会创建一个共享的bean实例。对于所有的bean请求,只要id与该bean定义的相匹配,那么Spring在每次需要时都返回同一个bean实例。
Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,singleton作用域是Spring中的缺省作用域。你可以在 bean 的配置文件中设置作用域的属性为 singleton,如下所示:
  1. <!-- A bean definition with singleton scope -->
  2. <bean id="..." class="..." scope="singleton">
  3.   <!-- collaborators and configuration for this bean go here -->
  4. </bean>
复制代码
单例的例子
1.首先创建一个bean。
  1. package com.spring.demo;
  2. public class SingletonBean{
  3. private String message;
  4. public void setMessage(String message){
  5.    this.message = message;
  6. }
  7. public void getMessage(){
  8.    System.out.println("Your Message : " + message);
  9. }
  10. }
复制代码
2.在Spring的配置文件中配置该bean。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.    xsi:schemaLocation="
  5.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.   <bean id="SingletonBean" class="com.spring.demo.SingletonBean" scope="singleton"></bean>
  7.   <!-- 或者 -->
  8.   <!-- <bean id="SingletonBean" class="com.spring.demo.SingletonBean" ></bean> -->
  9. </beans>
复制代码
测试该Bean是否为单例的。
  1. package com.spring.demo;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.junit.Test;
  5. public class TestBean {
  6.   @Test
  7.   public void textUser()
  8.   {
  9.     //1.获取spring文件
  10.     ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
  11.     //2.由配置文件返回对象
  12.     SingletonBean singletonBeanA = (SingletonBean)context.getBean("SingletonBean");
  13.     singletonBeanA.setMessage("I'm object A");
  14.     singletonBeanA.getMessage();
  15.     SingletonBean singletonBeanB = (SingletonBean)context.getBean("SingletonBean");
  16.     singletonBeanB.getMessage();
  17.   }
  18. }
复制代码
运行结果:


由于SingletonBean是单例的作用域,创建两个SingletonBean对象,第二个对象获取SingletonBean对象中的消息值得时候即使是由一个新的getBean()方法来获取,也可以不用设置对象中消息的值就可以直接获取SingletonBean中的消息,因为这时的消息已经由第一个对象初始化了。
在单例中,每个Spring IoC容器只有一个实例,无论创建多少个对象,调用多少次getMessafe( )方法获取它,它总是返回同一个实例。

(2)被声明为prototype的bean

当一个bean的作用域为prototype,表示一个bean定义对应多个对象实例。
声明为prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。
prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。
根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
prototype的例子。
还是上面的代码。其他代码不变,把Bean.xml文件中bean的作用域由singleton改为prototype。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="
  5.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.     <bean id="SingletonBean" class="com.spring.demo.SingletonBean" scope="prototype"></bean>
  7. </beans>
复制代码
执行代码,程序的执行结果为:


从图上可以看出在SingletonBeanA中设置的参数值在SingletonBeanB就获取不到了,说明这两个对象现在返回的就不是同一个对象实例。

(3)使用注解定义 bean 的作用域

除了在Bean.xml文件中定义bean的作用域之外,还可以使用注解来定义 bean 的作用域。
1.在Bean中加上注解。
  1. package com.spring.demo;
  2. import org.springframework.context.annotation.Scope;;
  3. import org.springframework.stereotype.Component;
  4. @Component("SingletonBean")
  5. @Scope("prototype")
  6. public class SingletonBean {
  7.     private String message;
  8.     public void setMessage(String message){
  9.         this.message  = message;
  10.     }
  11.     public void getMessage(){
  12.         System.out.println("Your Message : " + message);
  13.     }
  14. }
复制代码
    @Component("SingletonBean")注解是告诉Spring这是一个bean。@Scope("prototype")注解是告诉Spring该bean的作用域是prototype。
2.bean.xml文件修改一下。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  7.         http://www.springframework.org/schema/context
  8.         http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  9.     <context:component-scan base-package="com.spring.demo" />
  10. </beans>
复制代码
<context:component-scan base-package="com.spring.demo" />就是扫描com.spring.demo包中的所有类的注解。
测试代码不用变,运行测试。


和在bean.xml中直接定义bean和其作用域是一样的效果。其他作用域也可以使用注解方式声明bean的作用域。
request,session和application这三个作用域都是基于web的Spring WebApplicationContext实现的,只有在web环境下(比如XmlWebApplicationContext)中才能使用。 如果开发者仅仅在常规的Spring IoC容器中比如ClassPathXmlApplicationContext在中使用这些作用域,那么将会抛出一个IllegalStateException来说明使用了未知的作用域。
也就是当用户使用Spring的WebApplicationContext时,除了使用常规的singleton和prototype作用域之外,还可以使用另外3种Bean的作用域,即request,session和application。
在使用Web应用环境相关的Bean作用域时,必须在Web容器中进行一些额外的配置:
1.如果开发者使用了Spring Web MVC框架的话,每一个请求都会通过Spring的DispatcherServlet来处理,也就没有其他特殊的初始化配置,就不需要配置了。DispatcherServlet已经包含了相关的状态。
2.如果开发者使用的是低版本Web容器比如Servlet 2.5的web容器,请求不是通过Spring的DispatcherServlet(比如JSF或者Struts)来处理的。那么开发者需要注册org.springframework.web.context.request.RequestContextListener或者ServletRequestListener。可以在web.xml中增加如下的Listener声明:
  1. <web-app>
  2.     ...
  3.     <listener>
  4.         <listener-class>
  5.             org.springframework.web.context.request.RequestContextListener
  6.         </listener-class>
  7.     </listener>
  8.     ...
  9. </web-app>
复制代码
ServletContextListener只负责监听web容器启动和关闭的事件,而RequestContextListener实现了ServletRequestListener监听器接口,该监听器监听http请求事件。Web服务器接收到的每一次请求都会通知该监听器。
而在Servlet 3.0以后,这些都能够通过WebApplicationInitializer接口来实现配置。
3.如果不使用Listener,也可以考虑使用Spring的RequestContextFilter,通过http过滤器进行配置,在url-pattern中对所有的页面进行过滤。也是在web.xml中进行配置。
  1. <web-app>
  2.     ...
  3.     <filter>
  4.         <filter-name>requestContextFilter</filter-name>
  5.         <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
  6.     </filter>
  7.     <filter-mapping>
  8.         <filter-name>requestContextFilter</filter-name>
  9.         <url-pattern>/*</url-pattern>
  10.     </filter-mapping>
  11.     ...
  12. </web-app>
复制代码
配置完这些额外的配置之后,就可以使用另外的3种bean的作用域了。

(4)请求作用域

请求作用域参考如下的Bean定义
  1. <bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
复制代码
Spring容器会在每次用到loginAction来处理每个HTTP请求的时候都会创建一个新的LoginAction实例。也就是说,loginActionBean的作用域是HTTP Request级别的。
当http请求调用作用域为request的bean的时候,每增加一个HTP请求,Spring就会创建一个新的bean,在请求处理完成之后便及时销毁这个bean。
开发者可以随意改变实例的状态,因为通过loginAction请求来创建的其他实例根本看不到开发者改变的实例状态,所有创建的Bean实例都是根据独立的请求来的。

(5)会话作用域

会话作用域参考如下的Bean定义
  1. <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
复制代码
Spring容器会在每次调用到userPreferences时,在一个单独的HTTP会话周期来创建一个新的UserPreferences实例。换言之,userPreferencesBean的作用域是HTTP Session级别的。
Session中所有http请求共享同一个请求的bean实例。Session结束后就销毁bean。 在request-scoped作用域的Bean上,开发者可以随意的更改实例的状态。同样,使用从同一个userPreferences bean定义创建的其他HTTP Session实例在看不到不是自己的内部状态的修改,因为他们是单个的HTTP会话。
每个Session请求都会创建新的userPreferences实例,所以开发者更改一个Bean的状态,对于其他的Bean依旧是不可见的。

(6)全局作用域

全局作用域参考如下的Bean定义
  1. <bean id="appPreferences" class="com.foo.AppPreferences" scope="application"/>
复制代码
Spring容器会在整个web应用范围使用到appPreferences的时候创建一个新的AppPreferences的实例。也就是说,appPreferencesBean是在ServletContext级别的,作为常规的ServletContext属性。这种作用域在一些程度上来说和Spring的单例作用域相似,但是也有如下不同之处:
1.application作用域是每个ServletContext中包含一个,而不是每个SpringApplicationContext之中包含一个(某些应用中可能包含不止一个ApplicationContext)。
2.application作用域仅仅作为ServletContext的属性可见,单例Bean是ApplicationContext可见。
接下来再来简单的学习下在Spring当中如何自定义作用域:
在Spring 2.0中,Spring的Bean作用域机制是可以扩展的,这意味着,你不仅可以使用Spring提供的预定义Bean作用域,还可以定义自己的作用域,甚至重新定义现有的作用域(不提倡这么做,而且你不能覆盖内置的singleton和prototype作用域)

(7)自定义作用域

除了使用Spring已经定义好的作用域之外,还可以自定义bean的作用域。
要底线自定义作用域
1.首先需要实现自定义Scope类。
首先要先实现org.springframework.beans.factory.config.Scope这个接口,要将自定义scope集成到Spring容器当中就必须要实现这个接口。接口中有两个常用的方法,分别用于底层存储机制获取和删除这个对象。
2.在实现一个或多个自定义Scope并测试通过之后,接下来便是如何让Spring容器来识别新的作用域。registerScope方法就是在Spring容器中用来注册新的作用域。
  1. void registerScope(String scopeName, Scope scope);
复制代码
其中:第一个参数是与作用域相关的全局唯一的名称,第二个参数是准备实现的作用域的实例,就是实现Scope接口的实例。
比如实现Scope接口的类为SimpleThreadScope,要实现的自定义的bean的作用域的名称为“thread”,那就可以这么写。
  1. Scope threadScope = new SimpleThreadScope();
  2. beanFactory.registerScope("thread", threadScope);
复制代码
3.在实现和注册自定义的scope类之后,就可以通过如下类似的Bean定义来使用自定义的Scope:
  1. <bean id="..." class="..." scope="thread">
复制代码
另外,在自定义的Scope中,开发者也不限于仅仅通过编程方式来实现自定义的bean的作用域,也可以在Spring的配置文件中配置和使用自定义作用域和,比如配置CustomScopeConfigurer实例实现自定义的作用域,声明作用域名称为“thread”,就可以在xml文件中做如下类似的定义。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:aop="http://www.springframework.org/schema/aop"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop
  8.         http://www.springframework.org/schema/aop/spring-aop.xsd">
  9.     <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
  10.         <property name="scopes">
  11.             <map>
  12.                 <entry key="thread">
  13.                     <bean class="org.springframework.context.support.SimpleThreadScope"/>
  14.                 </entry>
  15.             </map>
  16.         </property>
  17.     </bean>
  18.     <bean id="bar" class="x.y.Bar" scope="thread">
  19.         <property name="name" value="Rick"/>
  20.         <aop:scoped-proxy/>
  21.     </bean>
  22.     <bean id="foo" class="x.y.Foo">
  23.         <property name="bar" ref="bar"/>
  24.     </bean>
  25. </beans>
复制代码
以上就是Spring Bean作用域的一些基本信息。仅为个人经验,希望能给大家一个参考,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行