日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java-Spring AOP日志和緩存

 印度阿三17 2019-10-26

我通過一個(gè)簡單的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 {

}

我使用這種機(jī)制來記錄這樣的方法的輸入和輸出(注意@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)= {}”時(shí),cachedService.getInt(1));第二次,使用緩存的值,但是不記錄輸入和輸出參數(shù),因?yàn)榫彺媸堑谝粋€(gè)包裝器.是否可以通過某種方式將LoggingAspect配置為第一個(gè)包裝器,以便我將能夠同時(shí)使用AOP日志記錄和Spring Cache?

解決方法:

只需實(shí)現(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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多