0
votes

I am new to Spring and having some problems trying to use the @Autowired annotation, trying to inject the Repository class inside PostController

@Controller
@RequestMapping("/posts")

public class PostController {


    @Autowired 
    private Repository repository;


    @RequestMapping(value="",method= RequestMethod.GET)
    public String listPosts(Model model){
        model.addAttribute("posts",repository.findAll());
        return "posts/lists";
    }
}

public interface Repository extends CrudRepository<Posts,Long> {
}

These are the error messages I get

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'postController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private domain.Repository controllers.PostController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [domain.Repository] 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:1208) 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:759) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689) at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) at org.springframework.boot.SpringApplication.run(SpringApplication.java:969) at org.springframework.boot.SpringApplication.run(SpringApplication.java:958) at MyApplication.main(MyApplication.java:7) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private domain.Repository controllers.PostController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [domain.Repository] 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 [domain.Repository] 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

4
Use a setter? or declare it as a dependency on the constructor?Dani Mesejo
Annotate your repository with @Repository (But pick a different more specific name for your interface)Erwin Bolwidt
Agree with @Erwin Bolwidt Please Annotate your repository interface with @Repository and change your repository interface name like PostsRepositoryMuhammad Siddique

4 Answers

1
votes

You have to create your own repository and extend it from repository, now you are using spring Repository interface.

@Repository
public interface PostRepository extends CrudRepository<Posts,Long> {

}

and @Autowire it

@Autowired 
private PostRepository repository;
0
votes

Looking at you log I suggest you to verify two points:

  • is domain.Repository annotated with @Component?
  • if the Application starts into the controllers package it will parse only the classes below the controllers package, that is excluding the domain package (where Repository is)

for the second point a quick fix you can either rearrange the packages under a myapp package and have the spring application start form a class at that level, or more generic configurations

0
votes

For good practices jpa, hibernate and especially for better handling of persistence exceptions you must add the @Repository annotation to all your repositories.

@Repository
public interface PostRepository extends CrudRepository<Posts,Long> {

}

Now to instantiate the repository class you must do it as public.

@Autowired 
PostRepository postRepository;

Mention also that as good practice you create a service to handle all the logic of the process, where your repository is used.

0
votes

The exception here is NoSuchBeanDefinitionException .

Based on the javadoc-api

public class NoSuchBeanDefinitionException
extends BeansException

Exception thrown when a BeanFactory is asked for a bean instance for which it cannot find a definition. This may point to a non-existing bean, a non-unique bean, or a manually registered singleton instance without an associated bean definition.

There are many possibilities that could lead to this exception. Please read through this detailed explanation to get a complete picture. -> What is a NoSuchBeanDefinitionException and how do I fix it?