java - Spring AOP 方面未执行

标签 java aop aspectj spring-aop aspects

我一直在试图找出为什么我的简单方面没有得到执行。我查看了类似问题的答案,但仍然无法使其工作。

我的目的是用 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) {
        }
    }
}

Spring 配置:

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

非常感谢任何建议。

最佳答案

所以我摆弄了一下这个并让它工作。我修改了如下方面:

@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();
    }
}

我还将 Spring 配置更改为:

<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"/>

关于java - Spring AOP 方面未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6448471/

相关文章:

java - 从 LTW 的属性文件中读取 Aspectj 切入点定义

spring - 为什么 aspect j 不能编织 show Xlint cantFindType

java - Eclipse:矛盾的警告

java - Optaplanner VRP 从工作解决方案中移除客户

java - 为什么解决方案是这样的?

java - ApplicationContext.getBean(Class clazz) 不适用于代理

java - 如何基于带注释的参数编写方面切入点

java - Java 中出现 "scan cannot be resolved"错误

java - 是否热衷于使用mp3spi获得mp3文件的持续时间?

java - Spring AOP : aspect @Around doesn't work