Pat*_*čin 7 spring spring-data spring-cache
鉴于我有 Spring Data 存储库并且我在方法上放置了Cacheable注释findAll:
@Repository
@CacheConfig(cacheNames = TEMPLATE_CACHE)
public interface TemplateRepository extends JpaRepository<Template, Long> {
@Override
@Cacheable
List<Template> findAll();
}
Run Code Online (Sandbox Code Playgroud)
Intellij IDEA 显示警告:
Spring Team recommends that you only annotate concrete classes (and methods of concrete classes) with the @Cache* annotation, as opposed to annotating interfaces.
You certainly can place the @Cache* annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies.
The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"),
then the caching settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a caching proxy, which would be decidedly bad.
Run Code Online (Sandbox Code Playgroud)
真的有那么糟糕吗?
从实践的角度来看会发生什么?
鉴于我想findAll在例如Controller映射中调用,我应该把那个注释放在哪里?
Spring Data 如何处理这个问题?对我来说似乎工作正常。
小智 2
正如其他人在这里所说的,最好有一个层用于从存储库获取数据。
在该服务层下,您将添加缓存注释。当您以某种意想不到的方式做事时,您会发现 Spring 的代理无法按预期运行。
可缓存注释(据我所知)依赖于编织来发挥作用。您对方法的调用将通过 Spring 框架拦截以提供缓存功能。如果不存在缓存命中,它将继续运行该方法。
这就是为什么将 Cacheable 注释放在正确的位置很重要:您可以在同一个类中调用缓存的方法,但由于编织的原因,实际上不会调用缓存。
Spring无法拦截同一个类中的调用。将其放在存储库接口上肯定会导致问题,因为 Spring 通过您正在使用的任何 Spring Data 包提供了具体的实现,这就是他们提供该警告的原因。
非常简短的解释: https://ducmanhphan.github.io/2019-02-19-How-Spring-Caching-mechanism-works/
Spring本身的详细信息: https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#cache
| 归档时间: |
|
| 查看次数: |
3681 次 |
| 最近记录: |