如何在配置中引用Spring.Net的ObjectFactory?

发布于 2024-07-24 09:07:08 字数 1043 浏览 11 评论 0原文

我想使用原型拦截器而不是单例拦截器,这样每个会话都会获得一个新的拦截器实例。

我查看了 HibernateTransactionManager 类,我认为“EntityInterceptorObjectName”和“ObjectFactory”是我必须设置的属性。

虽然 EntityInterceptorObjectName 非常明显,但是当它是“ObjectFactory 之母”时,我不知道如何引用 ObjectFactory,例如创建 AppContext/HibernateTransactionManager 的同一个 ObjectFactory。

配置的相关部分:

   <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="SessionFactory"/>
    <!-- the name of my non-Singleton EntityInterceptor-->
     <property name="EntityInterceptorObjectName" value="MyPrototypeEntityInterceptor" />
    <!-- What should I put as ref here? -->
    <property name="ObjectFactory" ref="" />
  </object>

  <object id="MyPrototypeEntityInterceptor" type="Hib.EntityInterceptor, Hib.Interceptors" singleton="false">
  </object>  

I would like to use prototype interceptors instead of a singleton interceptor so each session would get a new interceptor instance.

I looked into the HibernateTransactionManager Class and I think "EntityInterceptorObjectName" and "ObjectFactory" are the properties I have to set.

While EntityInterceptorObjectName is pretty obvious, I have no clue how to reference the ObjectFactory when it's the "mother of the ObjectFactories", e.g. the same ObjectFactory which creates the AppContext/the HibernateTransactionManager.

The relevant part of the config:

   <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="SessionFactory"/>
    <!-- the name of my non-Singleton EntityInterceptor-->
     <property name="EntityInterceptorObjectName" value="MyPrototypeEntityInterceptor" />
    <!-- What should I put as ref here? -->
    <property name="ObjectFactory" ref="" />
  </object>

  <object id="MyPrototypeEntityInterceptor" type="Hib.EntityInterceptor, Hib.Interceptors" singleton="false">
  </object>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰認得朕 2024-07-31 09:07:09

据我了解,您可以使用自己的 IObjectFactory 实现根据给定名称创建您想要的对象。 如果您希望能够使用 Spring-ApplicationContext 中定义的对象,您还可以实现 IObjectFactoryAware 将 ObjectFactory 连接到 Spring IoC 容器。

这应该使您能够做任何您想做的事情。 创建任何类的新实例,并使用 IoC 容器中的对象填充其属性(例如 SessionFactory,...)。

public class MyObjectFactoryAware : IObjectFactoryAware, IObjectFactory
{
   //this property will be set by the IoC container
   ObjectFactory {get; set;}

   //IObjectFactory implementation
   virtual object GetObject(string objectName)
   {
      MyEntityInterceptor interceptor = new MyEntityInterceptor();
      interceptor.SessionFactory = this.ObjectFactory.getObject("SessionFactory");
      return interceptor;

      //or of course if you just need it for wireing simply:
      //return this.ObjectFactory.getObject(objectName);
   }   
}

和映射:

<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate20">
  <property name="DbProvider" ref="DbProvider"/>
  <property name="SessionFactory" ref="SessionFactory"/>
  <!-- the name of my non-Singleton EntityInterceptor-->
  <property name="EntityInterceptorObjectName" value="MyPrototypeEntityInterceptor" />
  <!-- and the name of the objectFactory -->
  <property name="ObjectFactory" ref="MyObjectFactory" />
</object>

<object id="MyObjectFactory" type="Hib.MyObjectFactoryAware, Hib.Interceptors">
</object>

<object id="MyPrototypeEntityInterceptor" type="Hib.EntityInterceptor, Hib.Interceptors" singleton="false">
</object>

As i understand it you can use your own Implementation of IObjectFactory creating the objects you want according to the given name. If you want to be able to use the objects defined within your Spring-ApplicationContext you can also implement IObjectFactoryAware to connect your ObjectFactory to your Spring IoC Container.

This should enable you to do whatever you want. Create new instances of any class and fill its properties with objects from your IoC container (for example SessionFactory, ...).

public class MyObjectFactoryAware : IObjectFactoryAware, IObjectFactory
{
   //this property will be set by the IoC container
   ObjectFactory {get; set;}

   //IObjectFactory implementation
   virtual object GetObject(string objectName)
   {
      MyEntityInterceptor interceptor = new MyEntityInterceptor();
      interceptor.SessionFactory = this.ObjectFactory.getObject("SessionFactory");
      return interceptor;

      //or of course if you just need it for wireing simply:
      //return this.ObjectFactory.getObject(objectName);
   }   
}

and the mapping:

<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate20">
  <property name="DbProvider" ref="DbProvider"/>
  <property name="SessionFactory" ref="SessionFactory"/>
  <!-- the name of my non-Singleton EntityInterceptor-->
  <property name="EntityInterceptorObjectName" value="MyPrototypeEntityInterceptor" />
  <!-- and the name of the objectFactory -->
  <property name="ObjectFactory" ref="MyObjectFactory" />
</object>

<object id="MyObjectFactory" type="Hib.MyObjectFactoryAware, Hib.Interceptors">
</object>

<object id="MyPrototypeEntityInterceptor" type="Hib.EntityInterceptor, Hib.Interceptors" singleton="false">
</object>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文