Mic*_*hal 4 java aop spring caching
我正在通过一个简单的方面记录方法输入和输出参数。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是由这个注释附加的。
package com.mk.cache;
public @interface LoggedIO {
}
Run Code Online (Sandbox Code Playgroud)
我使用这种机制来记录这样的方法的输入和输出(注意@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我也使用 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));
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
LOG by AOP - invoking getInt([1])
Doing real work
LOG by AOP - result of getInt=1
cachedService.getInt(1)=1
cachedService.getInt(1)=1
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我LOGGER.info("cachedService.getInt(1)={}", cachedService.getInt(1));第二次调用时,会使用缓存值,但不会记录输入和输出参数,因为缓存是第一个包装器。是否有可能以某种方式将 LoggingAspect 配置为第一个包装器,以便我能够同时使用 AOP 日志记录和 Spring Cache?
小智 5
只需实现 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;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里阅读更多。第 11.2.7 章
| 归档时间: |
|
| 查看次数: |
1865 次 |
| 最近记录: |