我有这个基础存储库类:
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")
@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.