2009-06-21 3 views
0

그래서 각 세션은 새로운 인터셉터 인스턴스를 얻을 것 싱글 인터셉터 대신 프로토 타입 인터셉터를 사용하고 싶습니다.어떻게 설정에 Spring.Net의 ObjectFactory를 참조하십시오?

나는 HibernateTransactionManager를 클래스로 보면서 나는 "EntityInterceptorObjectName"와 "ObjectFactory를"나는 설정해야하는 특성이있다 생각합니다.

EntityInterceptorObjectName이 꽤 명백한 반면, "ObjectFactories의 어머니"인 경우 ObjectFactory을 참조하는 방법에 대한 단서가 없습니다. AppContext/HibernateTransactionManager를 생성하는 것과 동일한 ObjectFactory.

는 config의 관련 부분 : 난 당신이 주어진 이름에 따라 원하는 객체를 생성 IObjectFactory의 자신의 구현을 사용할 수 있습니다 알고있는 것처럼

<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> 

답변

1

. 당신이 당신의 내에서 정의 된 개체를 사용할 수 있도록하려면 봄을-의 ApplicationContext는 또한 스프링의 IoC 컨테이너에 ObjectFactory를 연결하는 IObjectFactoryAware을 구현할 수 있습니다.

이 당신이 원하는 무엇이든 할 수 있도록해야한다. 모든 클래스의 새로운 인스턴스를 만들고 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> 
+0

덕분에 zoidbeck : 난 그냥 HibernateTransactionManager를 이미 IObjectFactoryAware 그래서 난 전혀 ObjectFactory를 속성을 설정할 필요가 없습니다 구현하는 것을 알아 냈다! 저를 올바른 방향으로 가리켜 주셔서 감사합니다. – tobsen

+0

그래,이게 실제로 더 우아한 방법이야. 다행스럽게도 적어도 투표에 도움이 될 수 있습니다 :). – zoidbeck