Spring AOP日志记录和缓存

时间:2017-03-08 15:55:06

标签: java spring caching aop

我通过一个简单的方面记录方法输入和输出参数。

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;
    }
}

由此注释附加。

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。这是示例应用程序。

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

我的问题是,当我第二次调用LOGGER.info("cachedService.getInt(1)={}", cachedService.getInt(1));时,会使用缓存的值,但不会记录输入和输出参数,因为缓存是第一个包装器。是否有可能以某种方式将LoggingAspect配置为第一个包装器,因此我将能够同时使用AOP日志记录和两个Spring Cache?

1 个答案:

答案 0 :(得分:4)

只需实现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章