服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

开发:spring2.0中新的bean类型实现原理

我们知道,在spring2.0中,除了singleton及prototype两种类型的bean以外。默认情况下还增加了request、session及global session三种类型的bean,增加的三种类型的bean主要应用于web应用程序中。本文不打算分析三种类型的bean的用法,只是简单分析框架的实现原理。
  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);
    }

  默认情况下,低层的bean工厂中只支持singleton及prototype两种类型的bean。当把scope设置成request及session时将会出现不能正确识别scope的错误。这是因为普通的bean工厂都没有注册新的scope。只有在webapplicationcontext中注册才注册了新类型的bean。
  下面看实现注册scope的代码:
  在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
}

  结合上面的代码,现在应该明白为什么只有在web应用上下文中才能使用新增加三种类型的bean了吧。当然,由于有了scope,我们也可以非常轻松的实现我们自己的scope,增加新用户自定义类型的bean,然后设计出一个适合我们自己的bean工厂。