@Cacheable 注解爆红(不兼容的类型。实际为 java. lang. String‘,需要 ‘boolean‘)

发布于:2025-02-11 ⋅ 阅读:(69) ⋅ 点赞:(0)

在这里插入图片描述

    @Cacheable(value = "findPAUserById", key = "#id")
    public Optional<PAUser> findById(Integer id) {
        return paUserRepository.findById(id);
    }

我真的要笑死,findPAUserById 下面爆红波浪线,我以为是代码的问题,其实是注解导入的不正确,我真是醉了,我要导入的应该是 import org.springframework.cache.annotation.Cacheable;这个注解,结果导成了import javax.persistence.Cacheable;这个注解。


org.springframework.cache.annotation.Cacheablejavax.persistence.Cacheable 这两个注解的区别。

Feature org.springframework.cache.annotation.Cacheable javax.persistence.Cacheable
所属框架 Spring Framework (Spring Cache Abstraction) Java Persistence API (JPA)
作用 声明一个方法的结果可以被缓存。 Spring 会在方法调用时拦截,并检查缓存,返回缓存结果或执行方法。 声明一个实体类应该被缓存。 指示 JPA 提供者启用二级缓存。
应用范围 方法级别 实体类级别 (@Entity)
缓存层级 方法级的缓存,通常用于业务逻辑层, 可以缓存方法的返回值。 实体类的二级缓存,通常在持久层, 用于缓存实体类的数据。
主要功能 声明方法返回值可缓存,并指定缓存名称和键的生成规则。 使用 Spring 的缓存管理器来实际管理缓存。 声明实体类可以使用二级缓存。 JPA 提供者可能会使用自己的缓存实现,例如 Ehcache 或其他缓存。
缓存生命周期 由 Spring 的缓存管理器管理缓存的生命周期和失效策略。 由 JPA 提供者管理二级缓存的生命周期和失效策略。
缓存类型 可以搭配多种缓存提供者使用, 例如:内存缓存、Ehcache、Redis、Caffeine 等,并且可以自由切换。 由 JPA 提供者决定使用的二级缓存类型,通常与 JPA 提供者相关。
配置方式 通过 Spring Boot 的 application.propertiesapplication.yml 配置,以及 Spring 缓存注解 (@EnableCaching, @CacheManager, @CacheConfig等) 通过 JPA 提供者的配置文件 (persistence.xml 或其他 JPA 实现的配置) 或者注解,例如 shared-cache-mode
缓存控制 @CacheEvict, @CachePut, @Caching 等 Spring 缓存注解可以更精细地控制缓存的更新和删除。 通常使用 JPA 的 API 或者配置来管理二级缓存。 可以使用 JPA 的注解 @Cache 或者配置文件中指定缓存模式。
适用场景 适用于需要缓存方法结果,以提高性能的应用场景, 例如读取数据,计算结果等。 适用于需要缓存实体数据以减少数据库访问的场景。 例如,频繁读取的实体数据。
使用方式 @Cacheable(value = "cacheName", key = "#id") 标记一个可以缓存返回值的方法。 @Cacheable 标记一个实体可以使用二级缓存。
关键属性 value: 指定缓存名称。 key: 指定缓存键,通常使用 SpEL。condition: 表示缓存的条件。 unless: 表示不缓存的条件。 通常只使用@Cacheable, 有时会配合 shared-cache-mode使用, 用来指定缓存模式。
是否需要缓存管理器 必须 使用 Spring 缓存管理器 (CacheManager) 来管理实际的缓存。 通常需要配置 JPA 提供者的二级缓存配置。
例子 java @Cacheable(value = "userCache", key = "#id") public User getUserById(int id) { ... } java @Entity @Cacheable public class User { ... }

总结

  • org.springframework.cache.annotation.Cacheable: 用于方法级别的缓存,由 Spring 缓存抽象管理,重点是提高方法调用的性能。
  • javax.persistence.Cacheable: 用于实体级别的缓存,由 JPA 提供者管理,重点是减少数据库的访问,提高数据查询性能。

简而言之, 前者是 Spring Cache 的,用于缓存方法的返回值;后者是 JPA 的,用于缓存实体类数据。 两者从应用范围,使用场景,管理方式上都不同, 不要混淆。

1、org.springframework.cache.annotation.Cacheable

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.cache.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {
    @AliasFor("cacheNames")
    String[] value() default {};

    @AliasFor("value")
    String[] cacheNames() default {};

    String key() default "";

    String keyGenerator() default "";

    String cacheManager() default "";

    String cacheResolver() default "";

    String condition() default "";

    String unless() default "";

    boolean sync() default false;
}

2、javax.persistence.Cacheable

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.persistence;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Cacheable {
    boolean value() default true;
}


网站公告

今日签到

点亮在社区的每一天
去签到