0

I know this question has been asked so many times but I'm really having trouble to pick up the right solution. I have tried so many stuffs by seeing those questions but no one seems to fit my case.

The full log error is the following:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-09-20 12:28:25.613 ERROR 10152 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dashboardService in com.timmi.springBoot.controllers.RestEndPoints required a bean of type 'com.timmi.springBoot.service.DashboardService' that could not be found.


Action:

Consider defining a bean of type 'com.timmi.springBoot.service.DashboardService' in your configuration.

[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'restEndPoints': Unsatisfied dependency expressed through field 'dashboardService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.timmi.springBoot.service.DashboardService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
    at com.timmi.springBoot.controllers.Application.main(Application.java:20)
    ... 6 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.timmi.springBoot.service.DashboardService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
    ... 25 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

Here's my current implementation:

SpringBootApplication:

@SpringBootApplication
@EnableJpaRepositories(basePackages="com.timmi.springBoot.repositories")
@EntityScan(basePackages="com.timmi.springBoot.entities")
@EnableAutoConfiguration
@ComponentScan(basePackages="com.timmi.springBoot.service")
public class Application extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // TODO Auto-generated method stub
        return super.configure(builder);
    }
}

RestController:

@RestController
@RequestMapping("/app")
public class RestEndPoints {

    @Value("${default.course.name}")
    private String cName;

    @Value("${default.course.chapterCount}")
    private int chaptersCount;

    @Autowired
    private CourseConfiguration courseConfiguration;

    @Autowired
    private DashboardService dashboardService;

    @RequestMapping("/employees")
    public List<EmployeeInformation> getAllEmpl(){

        return dashboardService.getAllEmployee();

    }
}

DashboardService interface:

public interface DashboardService{

    List<CompanyRevenue> getTodayRevenueDash();

    List<ProductCategory> getBestCategory();

    List<OrderRecieved> getAllOrderRecieved();

    List<OrderCollectionStatus> getOrderCollection();

    List<EmployeeInformation> getAllEmployee();

    void addEmployee(EmployeeInformation employeeInformation);

    EmployeeInformation updateEmployee(EmployeeInformation employeeInformation);

    void deleteEmployee(EmployeeInformation employeeInformation);

}

class service :

@Service
public class DashboardServiceImpl implements DashboardService{

    @Autowired
    private CompanyRevenueRepository companyRevenueRepository;

    @Autowired
    private EmployeeInformationRepository employeeInformationRepository;

    @Autowired
    private OrderCollectionStatusRepository OrderCollectionStatusRepository;

    @Autowired
    private OrderRecievedRepository orderRecievedRepository;

    @Autowired
    private ProductCategoryRepository productCategoryRepository;

    @Override
    public List<CompanyRevenue> getTodayRevenueDash() {
        return companyRevenueRepository.findAll();
    }

    @Override
    public List<ProductCategory> getBestCategory() {
        return productCategoryRepository.findByBestCategory(true);
    }

    @Override
    public List<OrderRecieved> getAllOrderRecieved() {
        return orderRecievedRepository.findAll();
    }

    @Override
    public List<OrderCollectionStatus> getOrderCollection() {
        return OrderCollectionStatusRepository.findAll();
    }

    @Override
    public List<EmployeeInformation> getAllEmployee() {
        return employeeInformationRepository.findAll();
    }

    @Override
    public void addEmployee(EmployeeInformation employeeInformation) {
        employeeInformationRepository.save(employeeInformation);
    }

    @Override
    public EmployeeInformation updateEmployee(EmployeeInformation employeeInformation) {
        return employeeInformationRepository.save(employeeInformation);
    }

    @Override
    public void deleteEmployee(EmployeeInformation employeeInformation) {
        employeeInformationRepository.delete(employeeInformation);
    }

}
  • Can you try using `@ComponentScan` in your `Application.java`? – robot_alien Sep 20 '18 at 12:46
  • Possible Duplicate Question, https://stackoverflow.com/questions/41663652/consider-defining-a-bean-of-type-service-in-your-configuration-spring-boot – nitinsridar Sep 20 '18 at 12:46
  • @ComponentScan solves the issue, but it gives another problem using postman:{ "timestamp": "2018-09-20T12:58:10.955+0000", "status": 404, "error": "Not Found", "message": "No message available", "path": "/app/employees" } – ABDELJALIL TIMMI Sep 20 '18 at 12:58
  • 1
    To match this path, try adding a `@RequestMapping("/app")` annotation directly on your `RestEndPoints`class. – Arnaud Sep 20 '18 at 13:04
  • unfortunately, is the same issue – ABDELJALIL TIMMI Sep 20 '18 at 13:22
  • hi, can you now update your post with the code modifications specifically in your Application.java, so that we can find out what is causing the problem? – robot_alien Sep 20 '18 at 13:35
  • Doing these modifications, the only issue i get is: { "timestamp": "2018-09-20T13:45:20.302+0000", "status": 404, "error": "Not Found", "message": "No message available", "path": "/app/employees" } – ABDELJALIL TIMMI Sep 20 '18 at 13:44
  • Are your application & controller classes sitting in different packages? asking because @ComponentScan only scans for components in the same package.. – robot_alien Sep 20 '18 at 14:04
  • -src/main/java --com.timmi.springBoot.controllers Application.java RestEndPoints.java --com.timmi.springBoot.service DashboardService.java --com.timmi.springBoot.service.impl DashboardServiceImpl.java – ABDELJALIL TIMMI Sep 20 '18 at 14:39
  • Application and RestEndPoints are in the same package, DashboardService and DashboardServiceImpl are in two others package – ABDELJALIL TIMMI Sep 21 '18 at 10:51
  • 1
    Thank you guys, i solved the issue by moving the Application to the root package com.timmi.springBoot; best regards – ABDELJALIL TIMMI Sep 21 '18 at 11:33
  • Well, that's great news! :-) – robot_alien Sep 21 '18 at 12:21

2 Answers2

0

Remove @Repository and @Qualifier from your DashboardService and @Component from DashboardServiceImpl

And please get familiar with those annotations: What's the difference between @Component, @Repository & @Service annotations in Spring?

Kamil W
  • 2,230
  • 2
  • 21
  • 43
0

So the solution is to put the application class in root package, so here we don't need to make ComponantScan annotation

  • Yes, that worked because *"The SpringBootApplication annotation is equivalent to using Configuration, EnableAutoConfiguration and ComponentScan with their default attributes"*- excerpt from **Spring Official Documentation.** – robot_alien Sep 21 '18 at 12:38