启动期间@Cacheable和初始化

时间:2018-12-14 21:03:28

标签: spring spring-boot caching caffeine

我想在我的Spring Boot应用程序启动期间初始化缓存中的所有条目(从DB加载内容)。理想情况下,这是在应用程序准备就绪之前完成的。因此,我在@PostConstruct中实现了所有加载。我指出,尚未在@PostContruct中设置缓存,并且我遵循了一些技巧在ApplicationReadyEvent中进行此类初始化。但是,这仍然无法正常工作:

即使我已经在@Cacheable中调用了ApplicationReadyEvent方法,第二次调用仍会重新输入该方法,而不使用缓存。

我的服务:

@Service
public class MyService implements ApplicationListener<ApplicationReadyEvent {

  @Cacheable("entry")
  public List<String> getEntry() {
    System.out.println("getEntry called!");
    return Arrays.asList("aaa", "bbb");
  }

  @Override
  public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
    System.out.println("*** onApplicationEvent");
    getEntry();
  }
}

我的Caffeine CacheManager配置:

@Configuration
@EnableCaching
public class CachingConfig {

  @Bean
  public CacheManager cacheManager() {
    List<CaffeineCache> caffeineCaches = chacheList(Arrays.asList(
        "entry"
    ));

    SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
    simpleCacheManager.setCaches(caffeineCaches);
    System.out.println("*** @Bean CacheManager");
    return simpleCacheManager;
  }

  private List<CaffeineCache> chacheList(List<String> cacheNames) {
    return cacheNames.stream().map(s -> new CaffeineCache(s, Caffeine.newBuilder().build()))
        .collect(Collectors.toList());
  }

}

使用该服务的简单REST端点:

@RestController
public class MyController {

  @Autowired
  MyService myService;

  @GetMapping("/test")
  public void test()
  {
    System.out.println("*** GET /test");
    myService.getEntry();
  }
}

如果我启动应用程序并执行两个GET /test,则会得到以下输出:

INFO 20120  --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 907 ms
*** @Bean CacheManager
INFO 20120  --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
INFO 20120  --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
INFO 20120  --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.639 seconds (JVM running for 2.473)
*** onApplicationEvent
*** getEntry called!
INFO 20120  --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 20120  --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 20120  --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
*** GET /test
*** getEntry called!
*** GET /test

那么为什么MyService.getEntry的第二次调用(即“启动”之后的第一次调用)再次输入代码?

最后,我需要一个解决方案,该解决方案在应用程序完成启动之前执行第一次加载-即,我将尝试ContextRefreshedEvent或再次尝试@PostConstruct(和@Autowire CacheManager进行加载)在执行@PostConstruct之前配置)。但是第一步将是使此示例的行为符合预期。

1 个答案:

答案 0 :(得分:0)

好吧,愚蠢的错误:在我的服务中,对getEntry()的调用必须通过代理对象而不是直接进行:

@Service
public class MyService implements ApplicationListener<ApplicationReadyEvent {

  @Autowired
  MyService self;

  @Cacheable("entry")
  public List<String> getEntry() {
    System.out.println("getEntry called!");
    return Arrays.asList("aaa", "bbb");
  }

  @Override
  public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
    System.out.println("*** onApplicationEvent");
    self.getEntry();
  }
}