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

Spring Boot项目CurrencyExchangeRepository Bean注入失败问题求助

Spring Boot Bean注入失败排查与解决

问题场景

启动Spring Boot项目时失败,报错信息如下:

应用启动失败
描述:
com.hb.CurrencyExchangeSampleController中的字段repo需要类型为'com.hb.CurrencyExchangeRepository'的Bean,但未找到。
注入点带有以下注解:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)
    操作建议:
    考虑在配置中定义类型为'com.hb.CurrencyExchangeRepository'的Bean。
    进程已断开与目标VM的连接,地址:'127.0.0.1:53276',传输:'socket'
    进程退出码为0

用户提供的代码如下:

CurrencyExchangeRepository.java

package com.hb;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;

@Repository
@EnableJpaRepositories
public interface CurrencyExchangeRepository extends JpaRepository<CurrencyConversion, Long> {

    @Query("SELECT value FROM currency_conversion t WHERE t.from_country = ?1 and t.to_country = ?2")
    CurrencyConversion getCurrencyValue(String fromCurrency, String toCurrency);
}

CurrencyExchangeSampleController.java

package com.hb;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class CurrencyExchangeSampleController {

    @Autowired
    CurrencyExchangeRepository repo;

    
    @GetMapping("/chenna")
    public String msg(){
        return "hello chenna";
    }

    @GetMapping("/currency-exchange-service/fromCurrency/{fromCurrency}/toCurrency/{toCurrency}")
    public ExchangeValue retrieveExchangeValue(@PathVariable String fromCurrency,
                                               @PathVariable String toCurrency) {

        System.out.println("msg : " + msg);
        CurrencyConversion cc = impl.getCurrencyValue(fromCurrency, toCurrency);
        System.out.println("value : " + cc.getValue());
    }
}

CurrencyExchangeServiceSampleApplication.java

package com.hb;
@SpringBootApplication
@EnableDiscoveryClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ComponentScan("com.hb")
public class CurrencyExchangeServiceSampleApplication {

    public static void main(String[] args)
    {
        SpringApplication.run(
                CurrencyExchangeServiceSampleApplication.class,
                args);
    }
}

问题原因与解决办法

1. 禁用DataSource自动配置导致JPA Repository无法实例化

启动类中添加了@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}),直接禁用了Spring Boot的数据源自动配置。而JPA Repository的创建必须依赖数据源,没有数据源的情况下Spring无法生成CurrencyExchangeRepository的Bean实例。

解决:

  • 如果项目需要使用JPA操作数据库,移除@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class});
  • 在application.properties/application.yml中配置数据库连接信息,示例:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=db_user
spring.datasource.password=db_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

2. @EnableJpaRepositories注解位置错误

该注解被错误地添加在Repository接口上,它的正确位置是启动类,用来指定Spring扫描Repository接口的包路径。

解决:

  • 移除CurrencyExchangeRepository接口上的@EnableJpaRepositories注解;
  • 在启动类CurrencyExchangeServiceSampleApplication上添加@EnableJpaRepositories(basePackages = "com.hb"),确保Spring能扫描到Repository接口。

3. 其他潜在问题修正

  • Repository查询语句错误:getCurrencyValue方法的@Query只查询value字段,但返回类型是CurrencyConversion实体,会导致查询报错,修改为查询整个实体:
@Query("SELECT t FROM CurrencyConversion t WHERE t.from_country = ?1 and t.to_country = ?2")
CurrencyConversion getCurrencyValue(String fromCurrency, String toCurrency);
  • Controller方法错误:retrieveExchangeValue中使用了未定义的impl变量,应改为repo.getCurrencyValue;同时方法缺少返回值,需补充返回逻辑。

修正后的启动类示例

package com.hb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableDiscoveryClient
@EnableJpaRepositories(basePackages = "com.hb")
public class CurrencyExchangeServiceSampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(CurrencyExchangeServiceSampleApplication.class, args);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 20:40:31