据我所知,您可以使用自己的IObjectFactory实现,根据给定的名称创建所需的对象。如果希望能够使用Spring应用程序上下文中定义的对象,还可以实现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>