scope用来声明IOC容器中的对象应该处的限定场景或者说该对象的存活空间,即在IOC容器在 对象进入相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象。打个比方吧!我们都是处在社会 (容器)中,如果把中学教师作为一个类定义,那么当容器初始化这些类之后,中学教师只能局限在中学这个场景中,中学,就可以看做中学教师的scope。
Spring容器最初提供了两种bean的scope类型:singleton和 prototype,但发布2.0之后,又引入了另外三种scope类型,即request,session和global session类型。不过这三种类型有所限制,只能在web应用中使用,也就是说,只有在支持web应用的ApplicationContext中使用这 三个scope才是合理的。
singleton
SpringIoc容器只会创建该Bean的唯一实例,所有的请求和引用都只使用这个实例
Property
每次请求都创建一个实例
request
在一次Http请求中,容器会返回该Bean的同一个实例,而对于不同的用户请求,会返回不同的实例。需要注意的是,该作用域仅在基于Web的Spring ApplicationContext情形下有效,以下的session和global Session也是如此
session
同上,唯一的区别是请求的作用域变为了session
global session
全局的HttpSession中,容器会返回该bean的同一个实例,典型为在是使用portlet context的时候有效(这个概念本人也不懂)
注意:如果要用到request,session,global session时需要配置
在web.xml中添加:
<listener> <listener-class>org.springframework.web.context.scope.RequestContextListener /> </listener>
需要web.xml配置一个过滤器,如下:
<filter> <filter-name>XXXX</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> <filter-mapping> <filter-name>XXXX</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
另外,从2.0开始,可以自己定义作用域,但需要实现scope,并重写get和remove方法
一般情况下前面两种作用域是够用的,但如果有这样一种情况:
singleton类型的bean引用一个prototype的bean时会出现问题,因为singleton只初始化一次,但prototype每请求一次都会有一个新的对象,但prototype类型的bean是singleton类型bean的一个属性,理所当然不可能有新prototpye的bean产生,与我们的要求不符
解决方法:
1. 放弃Ioc,这与设计初衷不符,并代码间会有耦合
2. Lookup方法注入,推荐
但在用Lookup方法注入时也需要注意一点:需要在引用的Bean中定一个一个抽象地返回被引用对象的方法
Java测试代码(CurrentTime.java):
package com.huyong.lookup; import java.util.Calendar; public class CurrentTime { private Calendar now = Calendar.getInstance(); public void printCurrentTime() { System.out.println("Current Time:" + now.getTime()); } }
Java测试代码(LookupBean.java):
package com.huyong.lookup; public abstract class LookupBean { private CurrentTime currentTime; public CurrentTime getCurrentTime() { return currentTime; } public void setCurrentTime(CurrentTime currentTime) { this.currentTime = currentTime; } public abstract CurrentTime createCurrentTime(); }
XML配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="currentTime" class="com.huyong.lookup.CurrentTime" scope="prototype"/> <bean id="lookupBean" class="com.huyong.lookup.LookupBean" scope="singleton"> <lookup-method name="createCurrentTime" bean="currentTime"/> <property name="currentTime" ref="currentTime"/> </bean> </beans>
Java测试代码:
package com.huyong.lookup; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class LookupMain { public static void main(String[] args) throws Exception { ClassPathResource resource = new ClassPathResource("applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(resource); LookupBean lookupBean = (LookupBean) factory.getBean("lookupBean"); System.out.println("----------first time---------"); System.out.println("getCurrentTime:"); lookupBean.getCurrentTime().printCurrentTime(); System.out.println("createCurrentTime:"); lookupBean.createCurrentTime().printCurrentTime(); Thread.sleep(12345); System.out.println("---------second time---------"); System.out.println("getCurrentTime:"); LookupBean lookupBean02 = (LookupBean) factory.getBean("lookupBean"); lookupBean02.getCurrentTime().printCurrentTime(); System.out.println("createCurrentTime:"); lookupBean02.createCurrentTime().printCurrentTime(); } }