2011-01-20 2 views
3

컨트롤러를 프로토 타입 (non singleton)으로 정의해야하는 My Asp.Net MVC 응용 프로그램에서 Spring.Net을 사용합니다. 요청 범위 (각 요청에 대한 새 개체)가 있어야하는 개체가 있습니다. 그들을 내 컨트롤러에 삽입하는 방법이 있습니까?"요청"범위를 가진 객체를 프로토 타입 객체에 삽입

<object type="xx.CompanyController, xx" singleton="false"> 
    <property name="Service" ref="ServiceA" /> 
    </object> 

    <object id="ServiceA" type="xx.ServiceA, xx" scope="request"/>  
    <property name="ObjectB" ref="ObjectB" /> 
    </object> 

    <object id="ObjectB" type="xx.ObjectB, xx" scope="request"/> 

마찬가지로 컨트롤러를 제외한 모든 개체는 싱글 톤으로 처리됩니다. ObjectB는 동일한 인스턴스를 공유해야하는 다른 개체에서 참조하는 프로토 타입이므로 안됩니다. 컨트롤러에서 singleton = "false"를 제거하고 추가 scope = "request"도 작동하지 않습니다 (컨트롤러는 싱글 톤으로 처리됩니다).

나는 MvcApplicationContext

+0

나는 동일한 행동을 발견했습니다. 즉, ** request ** scope로 정의 된 객체는 ** singleton ** 객체의 종속성 인 경우 단일 범위로 "승격"됩니다. –

답변

0

당신은 당신의 자신과 ControllerFactory을 설정해야와 Spring.Net 1.3.1을 사용합니다.

protected void Application_Start() 
{ 
//... 
    ControllerBuilder.Current.SetControllerFactory(new IoCControllerFactory()); 
//... 
} 

IoCControllerFactory GetControllerInstance을 DefaultControllerFactory

에서
public class IoCControllerFactory : DefaultControllerFactory 

을 상속 오버라이드 (override) 할 필요가 IoCWorker

protected override IController GetControllerInstance (RequestContext requestContext, Type controllerType) 
    { 
     ObjectTypeUtility.ArgumentIsNull(controllerType, "controllerType", true); 
     if (!typeof(IController).IsAssignableFrom(controllerType)) 
      throw new ArgumentException(string.Format(
         "Type requested is not a controller: {0}", 
         controllerType.Name), 
         "controllerType");  

      IController controller = IoCWorker.Resolve(controllerType) 
             as IController;     
      return controller; 

    } 

IoCWorker 클래스는, 주입 내가 유니티를 사용하고 을 방법을 해결 가지고 내가 알 수 없다 구현 - 필요한 경우 공유 할 수 있습니다.

컨트롤러에 ctor inject가 작동합니다.

행운을 빈다.

+0

asp.net MVC에서 IoC (Spring.Net에서 이미 사용)가 가능하지만 (Spring-) 객체 정의의 다른 (Spring-) 범위에 관한 질문은 문제가되지 않습니다. – Fabiano