Unsatisfied dependency expressed through field ‘repo’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

Issue

This Content is from Stack Overflow. Question asked by user18665270

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> {
    //Now we can use JpaRepository’s methods: save(), findOne(), findById(), findAll(), count(), delete(), deleteById()…
    // without implementing these methods.

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


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());


    }
}


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);
    }
}

this is my code and i am getting below error

Field repo in com.harman.ExchangeServiceImpl required a bean of type ‘com.harman.CurrencyExchangeRepository’ that could not be found.

The injection point has the following annotations:
– @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:
Consider defining a bean of type ‘com.harman.CurrencyExchangeRepository’ in your configuration.

WARN 15232 — [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘exchangeServiceImpl’: Unsatisfied dependency expressed through field ‘repo’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.harman.CurrencyExchangeRepository’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-09-18 20:07:09.693 INFO 15232 — [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2022-09-18 20:07:09.725 INFO 15232 — [ restartedMain] ConditionEvaluationReportLoggingListener :



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?