考虑一个可以插入和检索对象并使用Spring缓存抽象的服务类,我如何才能以一种返回Optional的方式注释方法呢?
class MyServiceImpl implements MyService {
private static final String CACHE_NAME = "itemCache";
@Override
@Cacheable(CACHE_NAME)
public Optional<Item> findById(Long id) {
// access the repository to retrieve the item
}
@Override
@CachePut(cacheNames = CACHE_NAME, key = "#item.id")
public Item insertItem(Item item) {
...
}
}在上面的示例中,抛出了一个ClassCastException,因为insertItem将一个Item实例放在缓存中,而findById期望一个可能包含Item实例的Optional。
发布于 2016-05-09 13:37:50
只是对这篇评论的后续评论,给出了一个明确的答案。我们从Spring Framework4.3 RC2开始这样做
https://stackoverflow.com/questions/36844270
复制相似问题