在Spring CrudRepository上的自定义注释

时间:2018-11-02 13:11:45

标签: java spring annotations

我已经创建了用于统计目的的自定义注释,但是当与CrudRepository一起使用时,它不会被触发。

您对如何实现它有建议吗?

@Repository
public interface UserEntryForAuthRepository extends CrudRepository <UserEntryForAuth, String> {

   @Stat(endpoint = EndPoints.ORACLE, method = "get_password")
   @Transactional
   UserEntryForAuth findByUsername(String username);
}


@Retention(RetentionPolicy.RUNTIME)
public @interface Stat {
   EndPoints endpoint();
   String method();
}


@Around(value = "@annotation(stat)")
   public Object Stat(final ProceedingJoinPoint joinPoint, final Stat stat) throws Throwable {
      //String methodName = joinPoint.getSignature().getName();
      StopWatch stopWatch = new StopWatch();
      try {
         stopWatch.start();
         // invoke method
         Object returnValue = joinPoint.proceed();
         metricObserver.increment(CounterMetric.ACTION_COUNTER,stat.method(),"success");
         return returnValue;
      }
      catch (Exception e) {
         metricObserver.increment(CounterMetric.ACTION_COUNTER,stat.method(),"fail");
         throw e;
      }finally {
         stopWatch.stop();
         //LOGGER.debug("Name: {}, method: {}, elapased:{}",stat.endpoint().name(),stat.method(),stopWatch.getTotalTimeMillis() );
         metricObserver.observe(SummaryMetric.RESPONSE_TIME, stopWatch.getTotalTimeMillis(), stat.endpoint().name(), stat.method());
      }
   }

0 个答案:

没有答案