如何在配置中引用Spring.Net的ObjectFactory?
我想使用原型拦截器而不是单例拦截器,这样每个会话都会获得一个新的拦截器实例。
我查看了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您可以使用自己的 IObjectFactory 实现根据给定名称创建您想要的对象。 如果您希望能够使用 Spring-ApplicationContext 中定义的对象,您还可以实现 IObjectFactoryAware 将 ObjectFactory 连接到 Spring IoC 容器。
这应该使您能够做任何您想做的事情。 创建任何类的新实例,并使用 IoC 容器中的对象填充其属性(例如 SessionFactory,...)。
和映射:
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, ...).
and the mapping: