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

Spring Boot集成Solr启动失败:Repository Bean未找到问题求助

Spring Boot集成Solr启动报错:找不到Repository类型的Bean

报错信息

Parameter 0 of constructor in com.aa.aa.controller.ContentController required a bean of type 'com.aa.aa.repository.ContentDocRepository' that could not be found.

Action:
Consider defining a bean of type 'com.aa.aa.repository.ContentDocRepository' in your configuration.

问题原因分析

核心问题是Spring无法扫描到ContentDocRepository接口,无法自动生成对应的代理Bean,根源在于Solr配置类的@EnableSolrRepositories注解路径配置错误,同时存在几处代码细节问题。

解决方案

1. 修正Solr配置类的Repository扫描路径

你的ContentDocRepository实际包路径是com.aa.aa.repository,但原配置中basePackages写了错误的路径,同时配置类缺少@Configuration注解,Spring无法识别为配置类。修改后的SolrConfig代码:

@Configuration
@EnableSolrRepositories(basePackages = "com.aa.aa.repository")
@ComponentScan
public class SolrConfig {
    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient.Builder("http://localhost:8983/solr").build();
    }

    @Bean
    public SolrTemplate solrTemplate(SolrClient client) throws Exception {
        return new SolrTemplate(client);
    }
}

2. 清理Repository接口的冗余注解并修正查询方法名

  • 移除ContentDocRepository上多余的@Qualifier注解,Spring Data会自动为Repository生成Bean;
  • 查询方法名findByName不符合Spring Data命名规则,实体类字段是displayTitle,应改为findByDisplayTitle,否则无法正确映射Solr字段。修改后的Repository代码:
public interface ContentDocRepository extends SolrCrudRepository<ContentDoc, String> {
    List<ContentDoc> findByDisplayTitle(String displayTitle);
}

3. 确保Controller被Spring扫描

确认Spring Boot主启动类所在包为com.aa.aa或更上层,这样默认的@ComponentScan能覆盖控制器所在的com.aa.aa.controller包;若主启动类包层级不同,需在主启动类上显式添加@ComponentScan(basePackages = "com.aa.aa")。

完成以上修改后,重新启动应用,即可解决Bean找不到的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 18:40:32