【问题标题】:Spring Aspect on Converter转换器上的弹簧方面
【发布时间】:2018-08-25 08:09:41
【问题描述】:

我创建了一个简单的 Aspect 注释,用于测量注释方法的执行时间。 当我注释一个简单的 Spring Bean 的方法,注入 bean,并像 bean.annotatedMethod() 一样运行它时,一切正常。

但是,当我在 Spring Converter 上注释 convert() 方法时,注释被忽略。我猜原因是 convert() 是由 Spring 的 ConversionService 在内部调用的,并且以某种方式不尊重 Aspects。有什么办法让它工作吗?


注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecTime {

}

Aspect,我在 Spring 中注册的:

@Aspect
@Component
public class LogTimeAspect {

    @Around(value = "@annotation(annotation)")
    public Object LogExecutionTime(final ProceedingJoinPoint joinPoint, final LogExecTime annotation) throws Throwable {
        final long startMillis = System.currentTimeMillis();
        try {
            System.out.println("Starting timed operation");
            final Object retVal = joinPoint.proceed();
            return retVal;
        } finally {
            final long duration = System.currentTimeMillis() - startMillis;
            System.out.println("Call to " + joinPoint.getSignature() + " took " + duration + " ms");
        }

    }
}

这很好用:

@Component
public class Operator {

    @LogExecTime
    public void operate() throws InterruptedException {
        System.out.println("Performing operation");
        Thread.sleep(1000);
    }
}



@Bean
protected Void test(Operator o) {
    o.operate();

    return null;
}

但是这里,注释被忽略了:

public class SampleConverter implements Converter<SourceType, ResultType> {
    @Override
    @LogExecTime
    public ImmutableData convert(@Nonnull ClassifiedEvent result) {
       ...
    }
}



ConversionService conversionService;
...
conversionService.convert(source, ResultType.class));

【问题讨论】:

  • SampleConverter 是 Spring 托管的 bean 吗?
  • @EssexBoy 确实我错过了,我正在手动实例化我的转换器。解决了这个问题,谢谢!

标签: spring converter aspectj aspect


【解决方案1】:

@EssexBoy 的评论解决了,我的转换器不是弹簧管理的 bean。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    相关资源
    最近更新 更多