CustomizableTraceInterceptor

CustomizableTraceInterceptor

我有简单的Spring Data JPA存储库。

public interface UserRepository extends JpaRepository<UserEntity, Serializable>{ … }

有什么方法可以监视Spring生成的方法(例如findOne(…))的执行时间?

最佳答案

最简单的方法是使用CustomizableTraceInterceptor,如下所示:

@Configuration
@EnableAspectJAutoProxy
public class SpringDataExecutionLoggingConfiguration {

  @Bean
  public CustomizableTraceInterceptor customizableTraceInterceptor() {

    CustomizableTraceInterceptor customizableTraceInterceptor = new CustomizableTraceInterceptor();
    customizableTraceInterceptor.setUseDynamicLogger(true);
    customizableTraceInterceptor.setExitMessage("Executed $[methodName] in $[invocationTime]");
    return customizableTraceInterceptor;
  }

  @Bean
  public Advisor advisor() {

    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression("execution(public * org.springframework.data.jpa.repository.CrudRepository+.*(..))");
    return new DefaultPointcutAdvisor(pointcut, customizableTraceInterceptor());
  }
}

关于java - 如何记录Spring Data JPA存储库方法的执行时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25977660/

10-10 12:55