設定でSpring.NetのObjectFactoryを参照するにはどうすればよいですか?
-
06-07-2019 - |
質問
各セッションが新しいインターセプターインスタンスを取得できるように、シングルトンインターセプターの代わりにプロトタイプインターセプターを使用したいと思います。
HibernateTransactionManagerクラスを調べましたが、" EntityInterceptorObjectName"および" ObjectFactory"設定する必要があるプロパティです。
EntityInterceptorObjectNameはかなり明白ですが、 ObjectFactoriesのマザーである場合に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>
解決
私が理解しているように、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>
所属していません StackOverflow