Bean scopes主要是指如何创建bean对象,SPring通过配置文件而不是java类中定义Bean的scope。Spring已定定义了五种scope:singleton、prototype、request、session和globalSession。其中后三种只应用于基于web的
ApplicationContext。
1、singleton
Spring默认scope就是singleton。Spring只管理一份singleton bean的共享对象,该对象存储在singleton beans缓存中。 所有请求都从容器中返回同一个bean对象。
Spring中的singleton与GOF中的单例有有区别的。GOF中的单例在每个ClassLoader只会有一个对象。而Spring中的singleton是指每个Spring container中会有一个对象。在XML中定义如下:
<bean id="accountService" class="com.foo.DefaultAccountService"/> <!-- the following is equivalent, though redundant (singleton scope is the default) --> <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
2、prototype
不是singleton,每次请求返回的都是新创建对象。对于有状态的bean采用prototype而无状态的采用singleton.
说明:数据访问对象(DAO)通常不配置为原型,因为一个典型的DAO不会持有任何会话状态。
XML配置如下:
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
singleton bean中依赖prototype bean
根据IOC原则,Bean在实例化时已经把依赖的Bean注入,因此在singleton bean中,其依赖的
prototype bean是唯一的实例。这与设计的prototype相违背,解决办法是singleton bean中实现 ApplicationContextAware接口,当singleton bean中需要时都从 ApplicationContext中取 prototype bean。样例如下:package com.gll.spring.ioc.model;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Created by Administrator on 2015/4/5.
*/
public class ScopeExample implements ApplicationContextAware {
private ApplicationContext applicationContext;
private ProtoTypeExapmle protoTypeExapmle;
public ApplicationContext getApplicationContext() {
return applicationContext;
}
public ProtoTypeExapmle getProtoTypeExapmle() {
return protoTypeExapmle;
}
public void setProtoTypeExapmle(ProtoTypeExapmle protoTypeExapmle) {
this.protoTypeExapmle = applicationContext.getBean(ProtoTypeExapmle.class);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
package com.gll.spring.ioc.model;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2015/4/5.
*/
@Component
@Scope("prototype")
public class ProtoTypeExapmle {
}
3、Request scope
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>当每一次一个HTTP 请求loginAction 时,Spring容器会全新实例化LoginAction 对象。这就是Request 级别的scope,每一次请求都对应一个全新的LoginAction 对象,因而在请求过程中可任意改变LoginAction 实例的属性,当Request 结束后,其对应的LoginAction对象也销毁了。
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>Spring容器会为每一个活跃的http session创建一个全新的UserPreferences对象。与http session一起销毁。
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>表示在一个全局的HttpSession下会拥有一个单独的实例,通常用于Portlet环境下。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- an HTTP Session-scoped bean exposed as a proxy --> <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> <!-- this next element effects the proxying of the surrounding bean --> <aop:scoped-proxy/> </bean> <!-- a singleton-scoped bean injected with a proxy to the above bean --> <bean id="userService" class="com.foo.SimpleUserService"> <!-- a reference to the proxied userPreferences bean --> <property name="userPreferences" ref="userPreferences"/> </bean> </beans>