乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java-Spring AOP日志和緩存

       印度阿三17 2019-10-26

      我通過一個簡單的Aspect記錄方法的輸入和輸出參數(shù).

      package com.mk.cache;
      
      import org.aspectj.lang.ProceedingJoinPoint;
      import org.aspectj.lang.annotation.Around;
      import org.aspectj.lang.annotation.Aspect;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.stereotype.Component;
      
      import java.util.Arrays;
      
      @Aspect
      @Component
      public class LoggingAspect {
          @Around("within(@com.mk.cache.LoggedIO *) && execution(* *(..))")
          public Object logAroundPublicMethod(ProceedingJoinPoint joinPoint) throws Throwable {
              String wrappedClassName = joinPoint.getSignature().getDeclaringTypeName();
              Logger LOGGER = LoggerFactory.getLogger(wrappedClassName);
              String methodName = joinPoint.getSignature().getName();
          LOGGER.info("LOG by AOP - invoking {}({})", methodName, Arrays.toString(joinPoint.getArgs()));
          Object result = joinPoint.proceed();
          LOGGER.info("LOG by AOP - result of {}={}", methodName, result);
              return result;
          }
      }
      

      該注釋附加的內(nèi)容.

      package com.mk.cache;
      
      public @interface LoggedIO {
      
      }
      

      我使用這種機制來記錄這樣的方法的輸入和輸出(注意@LoggedIO):

      package com.mk.cache;
      
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.cache.annotation.Cacheable;
      import org.springframework.stereotype.Service;
      
      @Service
      @LoggedIO
      public class CachedService {
      
          private static final Logger LOGGER = LoggerFactory.getLogger(CachedService.class);
      
          @Cacheable("myCacheGet")
          public int getInt(int input) {
              LOGGER.info("Doing real work");
              return input;
          }
      }
      

      我也使用Spring Cache.這是示例應(yīng)用程序.

      package com.mk.cache;
      
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.CommandLineRunner;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cache.annotation.EnableCaching;
      
      @SpringBootApplication
      @EnableCaching
      public class CacheApplication implements CommandLineRunner {
      
          private static final Logger LOGGER = LoggerFactory.getLogger(CacheApplication.class);
      
          public static void main(String[] args) {
              SpringApplication.run(CacheApplication.class, args);
          }
      
          @Autowired
          private CachedService cachedService;
      
          @Override
          public void run(String... args) throws Exception {
              LOGGER.info("cachedService.getInt(1)={}", cachedService.getInt(1));
              LOGGER.info("cachedService.getInt(1)={}", cachedService.getInt(1));
          }
      }
      

      輸出看起來像這樣:

      LOG by AOP - invoking getInt([1])
      Doing real work
      LOG by AOP - result of getInt=1
      cachedService.getInt(1)=1
      cachedService.getInt(1)=1
      

      我的問題是,當(dāng)我調(diào)用LOGGER.info(“ cachedService.getInt(1)= {}”時,cachedService.getInt(1));第二次,使用緩存的值,但是不記錄輸入和輸出參數(shù),因為緩存是第一個包裝器.是否可以通過某種方式將LoggingAspect配置為第一個包裝器,以便我將能夠同時使用AOP日志記錄和Spring Cache?

      解決方法:

      只需實現(xiàn)spring Ordered接口,并在getOrder()方法中返回1.

      @Aspect
      @Component
      public class LoggingAspect implements Ordered {
          @Around("within(@com.mk.cache.LoggedIO *) && execution(* *(..))")
          public Object logAroundPublicMethod(ProceedingJoinPoint joinPoint) throws Throwable {
              String wrappedClassName = joinPoint.getSignature().getDeclaringTypeName();
              Logger LOGGER = LoggerFactory.getLogger(wrappedClassName);
              String methodName = joinPoint.getSignature().getName();
          LOGGER.info("LOG by AOP - invoking {}({})", methodName, Arrays.toString(joinPoint.getArgs()));
          Object result = joinPoint.proceed();
          LOGGER.info("LOG by AOP - result of {}={}", methodName, result);
              return result;
          }
      
          @Override
          public int getOrder() {
              return 1;
          }
      
      }
      

      閱讀更多here.第11.2.7章

      來源:https://www./content-1-522501.html

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多