1

私は自分の単純な側面が実行されない理由を理解しようとしてきました。私は同様の問題の答えを見ましたが、それでもそれを機能させることができません。

私の意図は、カスタムアノテーションが付けられたメソッドの実行を、メソッドの実行にかかる時間を追跡するAOPアドバイスでラップすることです。テストを実行すると、メソッドの出力が表示されますが、アドバイスは実行されていません(出力がログに記録されることを期待しています)。

Aspectクラスは次のとおりです。

@Aspect
class LatencyProfiler {

    private LatencyTrackerFactory factory = LatencyTrackerFactory.NOOP;

    @Pointcut(value="execution(@ProfileLatency * *(..)) && @annotation(annotation)", argNames="annotation")
    public void profiled(ProfileLatency annotation) {}

    @Around(value="profiled(annotation)", argNames="pjp,annotation")
    public Object profile(ProceedingJoinPoint pjp, ProfileLatency annotation) throws Throwable {

        ILatencyTracker tracker;

        try {
            tracker = factory.create(annotation.trackerName(), annotation.trackerNameSuffix());
        } catch (ConfigException e) {
            throw new RuntimeException(e);
        }

        tracker.begin();
        Object ret = pjp.proceed();
        tracker.end(null);

        return ret;
    }

   @Optional
    public void setFactory(LatencyTrackerFactory factory) {
        this.factory = factory;
    }
}

注釈が続きます:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ProfileLatency {
    String trackerName();
    String trackerNameSuffix() default"[unassigned]";
}

テストクラスが続きます:

public class Test {
    private static final Log LOG = LogFactory.getLog(Test.class);

    @PostConstruct
    public void init() {
        Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
                    @Override
                    public void run() {
                        for(int i = 0; i < 60; i++) {
                            foo();
                            LOG.info("HERE");
                        }
                    }
                }, 2000, TimeUnit.MILLISECONDS);

    }

    @ProfileLatency(trackerName = "latency", trackerNameSuffix = "s")
    public void foo() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {
        }
    }
}

スプリング構成:

<context:annotation-config/>
<aop:aspectj-autoproxy>
    <aop:include name="latencyProfileAspect"/>
</aop:aspectj-autoproxy>

<bean
    id    = "latencyLogger"
    class = "util.logging.LatencyLogger"
/>

<bean
    id    = "trackerFactory"
    class = "util.latency.LatencyTrackerFactoryImpl">
    <constructor-arg value = "config/latency-config.xml"/>
    <constructor-arg ref   = "latencyLogger"/>
</bean>

<bean
    id            = "latencyProfileAspect"
    class         = "util.latency.aop.LatencyProfiler"
    p:factory-ref = "trackerFactory"
/>

<bean id = "test" class="util.Test"/>

そして最後にテストの出力:

21:20:37,930 INFO  main/SpringMain - Ready.
21:20:40,928 INFO  pool-4-thread-1/Test - HERE
21:20:41,927 INFO  pool-4-thread-1/Test - HERE
21:20:42,926 INFO  pool-4-thread-1/Test - HERE
21:20:43,925 INFO  pool-4-thread-1/Test - HERE
21:20:44,924 INFO  pool-4-thread-1/Test - HERE
...

アドバイスをいただければ幸いです。

4

1 に答える 1

2

だから私はこれを少しいじって、それを機能させました。アスペクトを次のように変更しました。

@Aspect
public class LatencyProfiler {

    private static final Log LOG = LogFactory.getLog(LatencyProfiler.class);

    @Around("@annotation(annotation)")
    public Object profile(ProceedingJoinPoint pjp, ProfileLatency annotation) throws Throwable {

        ILatencyTracker tracker = ILatencyTracker.NOOP;

        try {
            tracker = StaticLatencyTrackerFactory.getTracker(annotation.trackerName(), annotation.trackerNameSuffix());
        } catch (Exception e) {
            LOG.error(e);
        }

        LatencyContext ctx = tracker.beginContext();
        Object ret = pjp.proceed();
        ctx.end();

        return ret;
    }

    /*
     *  special purpose factory method
     */
    public static LatencyProfiler aspectOf() {
        return MyAspectHolder.instance;
    }

    /**
     * private class holding the singleton
     */
    private static class MyAspectHolder {
        static final LatencyProfiler instance = new LatencyProfiler();
    }
}

また、スプリング構成を次のように変更しました。

<context:annotation-config/>
<aop:aspectj-autoproxy proxy-target-class="true"/>

<bean
    id             = "latencyProfileAspect"
    class          = "util.latency.aop.LatencyProfiler"
    factory-method = "aspectOf"
/>

<bean id = "test" class="util.Test"/>
于 2011-06-23T16:19:38.347 に答える