2009-09-14 3 views
1

내 프로젝트에서 로깅 목적으로 Spring.NET AOP를 사용하려하지만, 두 가지 어드 바이스가 훌륭하게 작동하지만 세 번째는 전혀 사용되지 않습니다. 여기 Spring.NET은 AOP AfterReturningAdvice를 사용하지 않습니다.

내 배선입니다 :

<!-- the "After" Advice --> 
<object id="GraphicsContextManagerAfter" type="PicturetoolWeb.App.Advice.GraphicsContextManagerAfter, PicturetoolWeb.App"> 
</object> 

<!-- The Proxy --> 
<object id="PicturetoolWeb.ImageLib.Context.GraphicsContextManager" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop"> 
<property name="target" ref="GraphicsContextManagerTarget"/> 
<property name="interceptorNames"> 
    <list> 
     <value>GraphicsContextManagerAfter</value> 
    </list>  
</property> 
</object> 

그때 봄에서 GraphicsContextManager 인스턴스를 얻을 : 나는 봄에서 개체를 얻기 위해 사용하는 ObjectManager의

var manager = ObjectManager.GetNew<GraphicsContextManager>(); 
// manager IS a proxy and has the advice set! 

var x = manager.DoSomeStuff(); 
// the DoSomeStuff Method is invoked, but my After advice is ignored 

중요한 부분 :

static ObjectManager() 
    { 
     Context = ContextRegistry.GetContext(); 
    } 

    public static T GetNew<T>() 
    { 
     return (T)Context.GetObject(typeof(T).FullName); 
    } 

봄도 예외는 아니지만 AfterAdvice도 무시됩니다. 어떤 아이디어? 내가 만든 다른 조언은 문제없이 작동했습니다.



_____________ 편집 : ______________

내 ObjectManager에 과부하를 추가는 :

public static T GetNew<T>(string typeFullName) 
    { 
     var ctx = GetContext(); 
     return (T)ctx.GetObject(typeFullName); 
    } 

그래서 저는 사용하는 경우

var contextManager = ObjectManager.GetNew<IGraphicsContextManager>("GraphicsContextManager"); 

Spring이 반환 한 인스턴스를 구체적인 유형이 아닌 인터페이스로 전송하면 예상대로 작동하고 내 조언이 사용됩니다 (예!).

하지만 왜 이해가 안되나요?

답변

0

Spring.NET AOP는 동적 프록시를 사용

  • 대상 클래스가 인터페이스를 구현하는 경우, 프록시는 인터페이스를 구현하고 대표는 대상 객체에 호출
  • 대상 클래스가 구현하지 않는 경우 인터페이스를 사용하면 프록시가 대상 클래스를 재정 의하여 프록시에서 재정의 할 가상 메서드로 표시해야합니다.

프록시 메커니즘 : http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism

HTH, 브루노