문제

로깅 목적으로 프로젝트에서 Spring.net AOP를 사용하려고 노력하고 있으며 두 가지 조언이 잘 작동하지만 세 번째는 전혀 사용되지 않습니다.

여기 내 배선이 있습니다.

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

그런 다음 Spring에서 GraphicsContextManager 인스턴스를 얻습니다.

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

봄부터 객체를 얻는 데 사용하는 ObjectManager의 중요한 부분 :

    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.net AOP는 동적 프록시를 사용합니다.

  • 대상 클래스가 인터페이스를 구현하는 경우 프록시는 인터페이스를 구현하고 대상 개체에 대한 대의원을 구현합니다.
  • 대상 클래스가 인터페이스를 구현하지 않으면 프록시는 대상 클래스를 무시하므로 프록시에 의해 과대 평가 될 메소드로 가상으로 표시해야합니다.

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

HTH, 브루노

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top