服务器启动时的Spring Boot缓存

时间:2018-12-12 08:00:42

标签: spring-boot caching spring-data-jpa

我发现了很多有关Spring Boot缓存的参考。

我能够在存储库方法级别实现。

@Cacheable(value = "booksByCategory", key = "#p0.categoryId", unless = "#result == null")
@EntityGraph(attributePaths = { "category" })
List<Book> findAllByCategoryId(Category category);

我想在服务器启动时而不是在第一次请求时缓存数据。 该怎么做?

1 个答案:

答案 0 :(得分:0)

如果您真的想“预加载”缓存,则可以为包含该方法的类的@PostConstruct方法中的每个预期类别调用findAllByCategoryId(category);例如:

local_bar

在初始化依赖项时,将在启动期间调用带有PostConstruct注释的方法。