我创建了一个简单的Aspect注释,用于测量带注释方法的执行时间 . 当我注释一个简单的Spring Bean的方法,注入bean,并像 bean.annotatedMethod()
一样运行它,一切正常 .
但是,当我在Spring Converter上注释 convert()
方法时,注释会被忽略 . 我猜测的原因是Spring的ConversionService在内部调用了 convert()
,并且某些方面不受尊重 . 有没有办法让它发挥作用?
注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecTime {
}
我在Spring注册的Aspect:
@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));
1 回答
通过@EssexBoy的评论解决,我的转换器不是一个 spring 托管bean .