狠扯蛋得一个问题,最近准备对一个查询操作进行aop拦截入缓存,于是写了个测试代码引入了aop
第一步:导入了依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
第二部:编写切面类,注解类
/**
* @description: 自定义缓存注解
* @create: 2021-11-17 10:13
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface CacheLog {
}
*/
@Aspect
@Component
public class RedisAspect {
/**
*注解含义:在添加了@CacheLog注解或者com.xuejf.spring.cloud.service这个包下面得所有类都进行拦截
*/
@Pointcut("@annotation(com.pro.spring.cloud.util.CacheLog)|| execution(public * com.pro.spring.cloud.service..*.*(..))")
public void getCacheConfig(){}
@Around("getCacheConfig()")
public Object invoke(ProceedingJoinPoint joinPoint){
Object proceed = null;
Object[] args = joinPoint.getArgs();
System.out.println("入参:"+Arrays.asList(args));
try{
proceed = joinPoint.proceed();
System.out.println("出参:"+proceed.toString());
}catch (Throwable throwable){
throwable.printStackTrace();
}
return proceed;
}
}
接口类引入注解进行切入
@Override
@CacheLog
public int getAl1(String id) {
Integer integer = Integer.valueOf(id);
int out = integer.intValue()*10;
return out;
}
然后进行调用得时候发现无法切入,网上查了很多,都说是没有导入包,没有导入依赖,但是我看了我得都引入了,然后没找到原因,并且这个切面类修改代码没反应,于是我记起来建这个类得时候用得是Aspect,然后改成得java类,然后我重新建了javaclass类,然后可以切入了,问题就出现在这个Aspect,所以大家乖乖得用java class类就可以了
AOP缓存拦截实战
本文分享了一次使用Spring Boot AOP实现缓存拦截的实际经验,重点介绍了如何通过自定义注解实现方法级别的缓存控制,并解决了在实施过程中遇到的问题。
1万+

被折叠的 条评论
为什么被折叠?



