Reputation: 305
I'm developing a movie rental web app in AngularJS, Spring and JPA. I've met NoSuchBeanDefinitionException but my code seems fine. I've been googling for hours and I can't find a solution.
movieRental package:
@ComponentScan(basePackages = { "movieManager" })
@SpringBootApplication
public class MovieRentalApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(
MovieRentalApplication.class, args);
new Context(ctx);
}
}
public class Context {
private static ApplicationContext context;
public Context(ApplicationContext applicationContext) {
context = applicationContext;
}
public static ApplicationContext getContext(){
return context;
}
}
movieManager package:
@Controller
public class AddMovieController {
@Autowired
AddMovie a;
@RequestMapping(value = "/addMovie", method = RequestMethod.POST)
public @ResponseBody void addMovie(@RequestBody Movie person) {
a = (AddMovie) Context.getContext().getBean(AddMovie.class);
a.run(person.getTitle());
}
}
@Service
public class AddMovie {
@Autowired
MovieRepository repository;
public void run(String valueFromPost) {
repository.save(new Movie(valueFromPost, "director"));
for (Movie movie : repository.findAll()) {
System.out.println(movie);
}
}
}
@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String director;
private String title;
protected Movie() {
}
public Movie(String title, String director) {
this.title = title;
this.director = director;
}
//getters/setters
}
@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {
List<Movie> findByTitle(String title);
}
And this is the error I get:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addMovie': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: movieManager.MovieRepository movieManager.AddMovie.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [movieManager.MovieRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at movieRental.MovieRentalApplication.main(MovieRentalApplication.java:13)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: movieManager.MovieRepository movieManager.AddMovie.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [movieManager.MovieRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [movieManager.MovieRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 18 more
Upvotes: 0
Views: 142
Reputation: 44535
It seems that you want to use Spring Data. In that case you don't need an implementation for your interface (the implementation will be automatically created at runtime by Spring Data). It's also not necessary to put the @Repository
annotation on your interface. However, to activate Spring Data, you have to put the annotation @EnableJpaRepositories
onto your MovieRentalApplication class (add the attribute basePackages, when the repository interfaces are in another package as the configuration class).
Upvotes: 1
Reputation: 3669
There is no Single implementation available for your
@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {
List<Movie> findByTitle(String title);
}
Hence the error. Create a class that implements this interface and make it as @Repository
like
public interface MovieRepositoryInterface extends CrudRepository<Movie, Long> {
List<Movie> findByTitle(String title);
}
@Repository
public class MovieRepository implements MovieRepositoryInterface<Movie, Long>{
}
Upvotes: 1