3

I'm creating a Spring boot application with one Controller, Service and Repository class.All these are defined below :

MainClass

@SpringBootApplication
public class MainClass  extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainClass.class);
    }

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

}

Controller:

@RestController
@EnableAutoConfiguration
public class ExampleController {

    public static final Logger logger = LoggerFactory.getLogger(ExampleController.class);

    private CustomReportService customReportService;

    @Autowired
    public void setCustomReportService(CustomReportService customReportService){
        this.customReportService = customReportService;
    }

    @RequestMapping(value="/api/report/list", method= RequestMethod.GET)
    public ResponseEntity<Collection<CustomReport>> listAllCustomReports(){
        return new ResponseEntity<>((Collection<CustomReport>) customReportService.listAllCustomReports(), HttpStatus.OK);
    }
}

Service Interface:

public interface CustomReportService {
    Iterable<CustomReport> listAllCustomReports();
}

Service Impl :

@Service
public class CustomReportServiceImpl implements CustomReportService{

    @Autowired
    private CustomReportRepository customReportRepository;

    @Autowired
    public void setCustomReportRepository(CustomReportRepository customReportRepository){
        this.customReportRepository = customReportRepository;
    }

    @Override
    public Iterable<CustomReport> listAllCustomReports() {
        return customReportRepository.findAll();
    }

}

Repository :

public interface CustomReportRepository extends CrudRepository<CustomReport, Long> {
}

On deployment, my app fails to start with the foll error :

2017-05-28 15:50:28.723 DEBUG 14708 --- [           main] o.s.b.d.LoggingFailure
AnalysisReporter   : Application failed to start due to an exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.services.customreport.CustomReportService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]

Please tell me where am i going wrong.

Thanks!

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
Manisha
  • 775
  • 6
  • 13
  • 30
  • Your class `CustomReportServiceImpl` is either not managed by Spring or is not discoverable by Spring. Check out `@ComponentScan` annotation in your configuration classes and edit it accordingly so that Spring container can find it. – Abdullah Khan May 28 '17 at 10:55
  • Let me know if it fixes it – Abdullah Khan May 28 '17 at 12:02
  • 1
    `@EnableAutoConfiguration` is only applicable on `@Configuration` classes. So in your case it doesn't do anything, also it is implied by the use of `@SpringBootApplication`. I suspect your `MainClass`f isn't in `com.test` but in some sub package of it. It is recommended to put the bootstrapping class in the highest package possible in your case `com.test` or whatever the actual package is. As `@ComponentScan` is implied by `@SpringBootApplication` as well however it starts from the package the `MainClass` is defined in not in other packages (hence the root package). – M. Deinum May 28 '17 at 13:01
  • @M.Deinum .you're correct. My main class was in a sub-package. When i put it in the root package, everything worked. yeah there is no need to add EnableAutoConfiguration annotation . When my service was not getting autowired, i just added it to see if that would make any difference. forgot to remove it. Thanks! – Manisha May 29 '17 at 08:12
  • @AbdullahKhan As suggested by Deinum when i moved my main class to root package, service injection worked. i need not add ComponentScan annotation then. Thanks for your help! – Manisha May 29 '17 at 08:14
  • Yes that's the correct way of fixing it. – Abdullah Khan May 29 '17 at 08:32

2 Answers2

3

Delete @EnableAutoConfiguration from controller, it is already in @SpringBootApplication. Your MainClass should be in the root package. Also try to add @ComponentScan("your.root.package") to your MainClass

Snowy
  • 69
  • 1
  • 9
2
  1. Make sure that you place your main Spring Boot application entry class MainClass at the top of your working package, with all other classes under.
  2. No need to add @EnableAutoConfiguration to your controller, as @SpringBootApplication already contains that.
  3. In CustomReportServiceImpl you are mixing field & constructor auto wiring for customReportRepository, choose only one.
Luay Abdulraheem
  • 751
  • 3
  • 11