狠扯蛋得一个问题,最近准备对一个查询操作进行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类就可以了