Spring2.0中新增了一个用来表示Bean范围的Scope接口 public interface Scope { Object get(String name, ObjectFactory objectFactory);//根据名称及创建工厂得到一个Bean实例 Object remove(String name);//删除一个指定名称的Bean } 在容器ConfigurableBeanFactory接口中定义了Bean工厂有关Scope注册的相关方法,使得可往Bean工厂中加入新类型的Bean。 public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, void registerScope(String scopeName, Scope scope);//往Bean工厂中添加一个新的范围(默认只有两种范围:singleton及prototype) void destroyScopedBean(String beanName);//销毁B ean工厂中范围Bean } 在AbstractFactoryBean的getBean方法中实现了对特定Scope Bean支持,核心代码摘要: String scopeName = mergedBeanDefinition.getScope();//取得当前Bean的范围,也即在定义中的scope=”request”的部分。 Scope scope = (Scope) this.scopes.get(scopeName);//得到Bean工厂中的范围处理器 if (scope == null) { throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'"); } try { //使用scope.get(beanName,ObjectFactory)从指定的范围中得到或创建Bean实例 Object scopedInstance = scope.get(beanName, new ObjectFactory() { public Object getObject() throws BeansException { beforePrototypeCreation(beanName);//前拦截 try { return createBean(beanName, mergedBeanDefinition, args);//调用子类的createBean实现真正的Bean创建工作 } finally { afterPrototypeCreation(beanName);//后拦截 } } }); bean = getObjectForBeanInstance(scopedInstance, name, mergedBeanDefinition);//返回正确类型的Bean实例 } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active", ex); } |
在WebApplicationContext中定义常量 public interface WebApplicationContext extends ApplicationContext { String SCOPE_REQUEST = "request"; String SCOPE_SESSION = "session"; String SCOPE_GLOBAL_SESSION = "globalSession"; } 然后在所有类型的Web应用上下文的实现中,都在Bean工厂的拦载过程中通过postProcessBeanFactory方法来注册新类型Scope,如GenericWebApplicationContext、StaticWebApplicationContext、AbstractRefreshableWebApplicationContext等WebApplication应用上下文实现中。 protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());//注册request类型的Bean beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));//注册session类型的Bean beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));//注册glogalsession 的Bean } |