代码之家  ›  专栏  ›  技术社区  ›  Ahmet Karakaya

弹簧积木上的自定义注释

  •  0
  • Ahmet Karakaya  · 技术社区  · 6 年前

    我已经为stat目的创建了自定义注释,但在与crudepository一起使用时不会触发它,如下所示。

    @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 回复  |  直到 6 年前