我是 Spring 框架的新手,正在尝试学习它。所以我找到了一些很好的视频教程,我觉得它很好解释。
有一个部分我无法理解,但我认为这与他在 Spring 中与缓存注释一起使用的 Spring EL 有关。
这是video我看了。
这是我想知道的代码部分。
@Override
@Transactional(
propagation = Propagation.REQUIRED,
readOnly = false)
@CachePut(
value = "greetings",
key = "#result.id")
public Greeting create(Greeting greeting) {
logger.info("> create");
if (greeting.getId() != null) {
logger.error(
"Attempted to create a Greeting, but id attribute was not null.");
throw new EntityExistsException(
"The id attribute must be null to persist a new entity.");
}
Greeting savedGreeting = greetingRepository.save(greeting);
logger.info("< create");
return savedGreeting;
}
我想知道他使用注释@CachePut
并使用“#result.id”
分配key
参数的部分。在视频中,他解释说它与响应值的 ID 进行映射。这意味着缓存将保存savedGreeting的ID。这是正确的吗?
它是如何工作的?我看过documentation对于Spring EL,也找不到相关内容。 Stackoverflow 的有人可能知道吗?
最佳答案
看看Spring Cache documentation 。这列出了使用缓存注释时可用的关键字。基本上:#result
是方法的返回值,可以在某些操作中使用,例如 @CachePut
。
这有意义吗?
关于java - 如何解释以下 Spring EL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637747/