如何在Java Spring中调用可缓存注解的“value”属性的方法

问题描述 投票:0回答:1

我有这个基础存储库类:

public abstract class FirestoreRepository<T> {
    protected Firestore db;

    protected FirestoreRepository(Firestore db) {
        this.db = db;
    }

    @Cacheable(value = , key = "#id")
    public Optional<T> findDocumentWithId(@Nonnull String id) throws InterruptedException, ExecutionException {
        DocumentSnapshot doc = db.collection(getCollection()).document(id).get().get();
        return Optional.ofNullable(doc.exists() ? doc.toObject(getEntityClass()) : null);
    }

    public abstract @Nonnull String getCollection();


...
}

我确实有多个继承自它的回购服务,

@Service
public class UserRepository extends FirestoreRepository<User> {

    @Autowired
    public UserRepository(Firestore db) {
        super(db);
    }
    
    @Override
    public @Nonnull String getCollection() {
        return FirestoreConstants.USERS_COLLECTIONS_NAME;
    }


...
}

我想调用 getCollection() 方法,例如在这里:

@Cacheable(value = "#getCollection()", key = "#id")

spring spring-boot spring-cache
1个回答
0
投票
 @Cacheable(value = "{#root.target.getCollection()}", key = "#id")

#root.target.getCollection() to call the getCollection() method. In this case, root target object would be the UserRepository object.
© www.soinside.com 2019 - 2024. All rights reserved.