Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Spring Boot

I am working on a spring boot application. I want to achieve sending some json data to a web api. When I try to run it, the following error comes up. After so many attempts, its not getting resolved. Any help will be much appreciated:

Error console:

2017-03-31 12:00:36.634  WARN 2972 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oniSavingsApiController': Unsatisfied dependency expressed through field 'oniSavingsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oniSavingsApiService': Unsatisfied dependency expressed through field 'readSheet'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.oni.excelReader.ReadSheet' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-03-31 12:00:36.634  INFO 2972 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-31 12:00:36.654  INFO 2972 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-31 12:00:36.798 ERROR 2972 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

APPLICATION FAILED TO START

Description:

Field readSheet in com.oni.service.OniSavingsApiService required a bean of type 'com.oni.excelReader.ReadSheet' that could not be found.

Application

@SpringBootApplication
@ComponentScan(basePackages = "com.oni.controller, com.oni.service")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Controller

@Controller
public class OniSavingsApiController {

    @Autowired
    private OniSavingsApiService oniSavingsService;

    @RequestMapping("/")
    public void home() {
        oniSavingsService.oakRestCall();
    }
}

Service

@Component
public class OniSavingsApiService {

@Autowired
private ReadSheet readSheet;

public static final String CUSTOM_INFO = "custominformation";
public static final String AUTHORIZATION = "Basic";

public void oakRestCall() {

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", AUTHORIZATION);

    ArrayList<ResponseEntity<ResponseData>> responses = new ArrayList<ResponseEntity<ResponseData>>();
    List<ExcelFields> listOfExcelData = readSheet.getFileContent();

    for (ExcelFields ef : listOfExcelData) {
        System.out.println(ef.getId());
        try {
            String json = "";
            String urlString = "";
            /**
            some code here
            **/
        } catch (Exception e) {
        }
    }
}
}
like image 213
r.kothari Avatar asked Oct 15 '25 03:10

r.kothari


1 Answers

Your ComponentScan scans only com.oni.controller and com.oni.service packages to inject the dependencies, so change it to scan through the whole "com.oni" package as shown below:

@ComponentScan(basePackages = "com.oni")

or the other option is include com.oni.excelReader package (in which your ReadSheet class is located) as well:

@ComponentScan(basePackages = "com.oni.controller, 
                           com.oni.service, com.oni.excelReader")

P.S.: If you move your Application class to com.oni, you don't need @ComponentScan at all, Spring boot automatically scans all sub-packages of com.oni for you.

like image 97
developer Avatar answered Oct 16 '25 19:10

developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!