5

Spring AOP を使用してロギングを実装するために、次の簡単な手順に従いました。しかし、それは機能していないようです。どんな助けでも役に立ちます

1) MyLoggingAspectクラスの作成

    import org.aspectj.lang.ProceedingJoinPoint;

public class MyLoggingAspect
{

    public MyLoggingAspect() {
        super();
        System.out.println("Instantiated MyLoggingAspect");     
    }

    public Object log(ProceedingJoinPoint call) throws Throwable
    {
        System.out.println("from logging aspect: entering method [" + call.toShortString()
                            +"] with param:"+call.getArgs()[0] );

        Object point =  call.proceed();

        System.out.println("from logging aspect: exiting method [" + call.toShortString()   
                            + "with return as:" +point);        

        return point;
    }

}

2)ログが必要なクラス( TixServiceImpl )を作成しました

public class TixServiceImpl implements TixService{

    @Override
    public void calculateSomething() {
        String s = "did some calculation..";
        System.out.println(s);
    }

    @Override
    public String getTixName() {
        return null;
    }
}

3) spring-aspectj.xmlファイルを作成

<beans...    
    <bean id="LoggingAspect"  class = "MyLoggingAspect"/>
    <aop:config>
          <aop:aspect ref="LoggingAspect">
             <aop:pointcut id="myCutLogging"
                    expression="execution(* TixService*.*(..))"/>
             <aop:around pointcut-ref="myCutLogging" method="log"/>
          </aop:aspect>
    </aop:config>    
</beans>

4) 簡単なテスト クライアント ( TixClient )を作成しました。

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TixClient {

    public static void main(String[] a){

        ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring-aspectj.xml");

        TixService tix = new TixServiceImpl();
        tix.calculateSomething();
        String s = tix.getTixName();

        System.out.println("End of the the client invocation!!"); 
    }   
}

5)次の出力が得られます

...
Instantiated MyLoggingAspect
did some calculation..
End of the the client invocation!!
4

2 に答える 2

16

私はあなたのコードを調べているだけですが、問題は、TixServiceImplSpringからインスタンスを取得しておらず、自分で手動でインスタンス化しているということですTixClientTixServiceSpringが返されたインスタンスにアスペクトを設定できるように、SpringApplicationContextから取得したSpringBeanである必要があると思います。

于 2009-01-18T20:46:14.080 に答える
2

Scott Bale の言うとおりです。Spring に TixServiceImpl をインスタンス化させてください。また、このような場合、Springs のログを有効にすると役立つ可能性があります。これにより、アスペクト/エイスのターゲットがいくつ見つかったかがわかるためです。

于 2009-01-18T21:23:36.767 に答える