Spring-如何使用AspectJ缓存自调用? [英] Spring - How to cache in self-invocation with aspectJ?

查看:85
本文介绍了Spring-如何使用AspectJ缓存自调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢您点击我的问题.我想在自调用中调用缓存方法,因此我需要使用AspectJ.(缓存的配置还可以)

Thank you to click my question. I want to call a caching method in self-invocation, so I need to use AspectJ. (cache's config is okay)

  1. 添加AspectJ依赖项

implementation 'org.springframework.boot:spring-boot-starter-aop'

  1. 将@EnableCaching(mode = AdviceMode.ASPECTJ)添加到我的application.java

@EnableJpaAuditing
@EnableCaching(mode = AdviceMode.ASPECTJ) // <-- here 
@SpringBootApplication
public class DoctorAnswerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DoctorAnswerApplication.class, args);
    }

}

  1. 我的service.java

@Service
public class PredictionService {

    @Cacheable(value = "findCompletedRecordCache")
    public HealthCheckupRecord getRecordComplete(Long memberId, String checkupDate) {
        Optional<HealthCheckupRecord> recordCheckupData;
        recordCheckupData = healthCheckupRecordRepository.findByMemberIdAndCheckupDateAndStep(memberId, checkupDate, RecordStep.COMPLETE);

        return recordCheckupData.orElseThrow(NoSuchElementException::new);
    }
}

  1. 我的测试代码

    @Test
    public void getRecordCompleteCacheCreate() {
        // given
        Long memberId = (long)this.testUserId;
        List<HealthCheckupRecord> recordDesc = healthCheckupRecordRepository.findByMemberIdAndStepOrderByCheckupDateDesc(testUserId, RecordStep.COMPLETE);
        String checkupDate = recordDesc.get(0).getCheckupDate();
        String checkupDate2 = recordDesc.get(1).getCheckupDate();

        // when
        HealthCheckupRecord first = predictionService.getRecordComplete(memberId,checkupDate);
        HealthCheckupRecord second = predictionService.getRecordComplete(memberId,checkupDate);
        HealthCheckupRecord third = predictionService.getRecordComplete(memberId,checkupDate2);

        // then
        assertThat(first).isEqualTo(second);
        assertThat(first).isNotEqualTo(third);
    }

我没有...什么?我没有上任何与AspectJ相关的课程.我认为 @EnableCaching(mode = AdviceMode.ASPECTJ)通过AspectJ而不是Spring AOP(proxy)使@Cacheable工作.

What did I don't...? I didn't make any class related with aspectJ. I think @EnableCaching(mode = AdviceMode.ASPECTJ) make @Cacheable work by AspectJ instead Spring AOP(proxy).

推荐答案

您是否已阅读

Did you read the Javadoc for EnableCaching?

请注意,如果将mode()设置为 AdviceMode.ASPECTJ ,则 proxyTargetClass()属性的值将被忽略.还要注意,在这种情况下, spring-aspects 模块JAR必须存在于类路径中,通过编译时编织或加载时编织将方面应用于受影响的类.在这种情况下不涉及任何代理;本地电话也会被拦截.

Note that if the mode() is set to AdviceMode.ASPECTJ, then the value of the proxyTargetClass() attribute will be ignored. Note also that in this case the spring-aspects module JAR must be present on the classpath, with compile-time weaving or load-time weaving applying the aspect to the affected classes. There is no proxy involved in such a scenario; local calls will be intercepted as well.

所以请检查您是否

  1. 在类路径上具有 spring-aspects ,并且
  2. 使用参数 java -javaagent:/path/to/aspectjweaver.jar 启动您的应用程序.
  1. have spring-aspects on the class path and
  2. started your application with the parameter java -javaagent:/path/to/aspectjweaver.jar.

#2可以替代,但是使用Java代理是最简单的.我不是Spring用户,所以我不是Spring配置方面的专家,但是即使像我这样的Spring noob也成功使用Java代理,所以请先尝试一下.

There is an alternative to #2, but using the Java agent is the easiest. I am not a Spring user, so I am not an expert in Spring configuration, but even a Spring noob like me succeeded with the Java agent, so please give that a shot first.

这篇关于Spring-如何使用AspectJ缓存自调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆