You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.7.5中MongoDB反应式仓库自定义查询报错:找不到aaa属性

Spring Data Reactive MongoDB自定义查询报错:No property 'aaa' found for type 'Simulation'

问题现象

启动Spring Boot 2.7.5项目时,使用ReactiveMongoTemplate实现自定义查询出现如下异常:

Could not create query for public abstract reactor.core.publisher.Flux it.reply.evtb.repository.specialization.PersSimRepo.aaa(java.lang.String,org.springframework.data.domain.Sort,java.util.List); Reason: No property 'aaa' found for type 'Simulation'; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property 'aaa' found for type 'Simulation'

原因分析

Spring Data对自定义Repository实现类的命名有强制规范:必须是**「自定义接口名+Impl」**。如果命名不符合规则,Spring会把自定义接口中的方法当成基于属性的查询方法,尝试解析方法名生成查询语句,最终因找不到对应属性报错。

你的代码中:

  • 自定义接口名为PersSimRepo
  • 实现类却命名为PerSimRepoImpl(少了字母s,不符合命名规范)

解决方案

  1. 修改实现类名称,使其严格遵循Spring Data的命名规则:
@Slf4j
public class PersSimRepoImpl implements PersSimRepo {

    private final ReactiveMongoTemplate mongoTemplate;

    public PersSimRepoImpl(ReactiveMongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public Flux<Simulation> aaa(String userId, Sort sort, List<GlobalSimStatus> status) {

        log.info("status:{}", status);
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("status").in(status));

        return mongoTemplate.find(query.with(sort), Simulation.class);
    }

}
  1. 确保实现类处于Spring容器的扫描范围内(如果不在主Application类的包或子包下,需手动配置扫描路径)

补充说明

Spring Data识别自定义Repository实现的逻辑是:当主Repository接口继承自定义接口后,会自动查找名称为「自定义接口名+Impl」的实现类,将其中的方法作为自定义实现处理。如果命名不匹配,Spring会默认按方法名解析规则处理,导致上述错误。

内容的提问来源于stack exchange,提问作者Mex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 19:20:35