callicoder / spring-boot-mysql-rest-api-tutorial

Building a Restful CRUD API using Spring Boot, Mysql, JPA and Hibernate
https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/
418 stars 439 forks source link

Unsatisfied Dependency Exception #3

Open Dhursh opened 6 years ago

Dhursh commented 6 years ago

Hi callicoder,

I am getting below error while trying to run the application, Stacktrace is below and i have attached the project folder structure as well.. please help me in getting this fixed,

Thanks in advance :)


APPLICATION FAILED TO START


Description:

Field noteRepository in com.example.easynotes.controller.NoteController required a bean of type 'com.example.easynotes.repository.NoteRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.easynotes.repository.NoteRepository' 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 'noteController': Unsatisfied dependency expressed through field 'noteRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.easynotes.repository.NoteRepository' 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.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.example.easynotes.EasyNotesApplication.main(EasyNotesApplication.java:12) ... 6 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.easynotes.repository.NoteRepository' 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) ... 24 more [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.519 s [INFO] Finished at: 2018-08-09T18:34:35+05:30 [INFO] Final Memory: 37M/412M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.4.RELEASE:run (default-cli) on project easy-notes: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'noteController': Unsatisfied dependency expressed through field 'noteRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.easynotes.repository.NoteRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} -> [Help 1]

image

callicoder commented 6 years ago

Hi,

Can you verify that you have @Repository annotation on the NoteRepository interface?

Dhursh commented 6 years ago

Yeah... i have the annotation.. please find the below code snippet of my NoteRepository interface

package com.example.easynotes.repository;

import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;

import com.example.easynotes.model.Note;

@Repository public interface NoteRepository extends JpaRepository<Note, Long> {

}

dtsw commented 6 years ago

Repository Annotation should be first letter uppercase.

You probably need to make sure your Repository gets scanned.

Since i created my Repository and Entity classes in a different package, i had to use the following two Annotations on the Main App class to make the classes scanned properly:

@EnableJpaRepositories(basePackages = {"mypkg"})

@EntityScan(basePackages = {"mypkg"})

arun-ammasai commented 3 years ago

Hey appreciate it man, stuck up with this issue got it resolved

ramazansakin commented 11 months ago

Hi @arun-ammasai , @dtsw , @Dhursh ,

If you still interested about the repo, I just create a PR to solve all the problems and upgrade jdk & spring boot.

https://github.com/ramazansakin/spring-boot-mysql-rest-api-tutorial/tree/master