假设我有Spring Data存储库并在findAll
方法上放置Cacheable注释:
@Repository
@CacheConfig(cacheNames = TEMPLATE_CACHE)
public interface TemplateRepository extends JpaRepository<Template, Long> {
@Override
@Cacheable
List<Template> findAll();
}
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.
真的那么糟糕吗?
从实际角度来看,会发生什么事情?
如果我想在例如Controller
映射中调用findAll
,我应该把注释放在哪里?
Spring Data 如何处理这个问题? 对我来说它似乎运行得很好。