Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Autowire @Repository annotated interface in Spring Boot

I'm developing a spring boot application and I'm running into an issue here. I'm trying to inject a @Repository annotated interface and it doesn't seem to work at all. I'm getting this error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootRunner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] 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:1202)     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 com.pharmacy.config.SpringBootRunner.main(SpringBootRunner.java:25) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] 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 common frames omitted Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] 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 common frames omitted 

Here is my code:

Main application class:

package com.pharmacy.config;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan;   @SpringBootApplication @ComponentScan("org.pharmacy") public class SpringBootRunner {       public static void main(String[] args) {         SpringApplication.run(SpringBootRunner.class, args);     } } 

Entity class:

package com.pharmacy.persistence.users;  import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;    @Entity public class UserEntity {      @Id     @GeneratedValue     private Long id;     @Column     private String name;  } 

Repository interface:

package com.pharmacy.persistence.users.dao;  import com.pharmacy.persistence.users.UserEntity; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository;   @Repository public interface UserEntityDao extends CrudRepository<UserEntity,Long>{  } 

Controller:

package com.pharmacy.controllers;  import com.pharmacy.persistence.users.UserEntity; import com.pharmacy.persistence.users.dao.UserEntityDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;   @RestController public class HomeController {       @Autowired     UserEntityDao userEntityDao;      @RequestMapping(value = "/")     public String hello() {         userEntityDao.save(new UserEntity("ac"));         return "Test";      } } 

build.gradle

buildscript {     ext {         springBootVersion = '1.2.2.RELEASE'     }     repositories {         mavenCentral()     }     dependencies {         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")     } }  apply plugin: 'java' apply plugin: 'idea' apply plugin: 'spring-boot' mainClassName = "com.pharmacy.config.SpringBootRunner" jar {     baseName = 'demo'     version = '0.0.1-SNAPSHOT' }   repositories {     mavenCentral() }   dependencies {     compile("org.springframework.boot:spring-boot-starter-data-jpa")     compile("org.springframework.boot:spring-boot-starter-web")     compile("org.springframework.boot:spring-boot-starter-ws")     compile("postgresql:postgresql:9.0-801.jdbc4")      testCompile("org.springframework.boot:spring-boot-starter-test") } 

application.properties:

spring.view.prefix: / spring.view.suffix: .html  spring.jpa.database=POSTGRESQL spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=update   spring.datasource.driverClassName=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/postgres spring.datasource.username=postgres spring.datasource.password=abc123 

I even compared my code with Accessing data jpa, and I'm running out of ideas what's wrong with this code. Any help appreciated. Thanks in advance.

EDITED: I changed my code as suggested to look like above, and I'm not getting that error when I'm injecting my @Repository interface into another component. However, I have a problem now - my component cannot be retrieved (I used debugging). What I'm doing wrong so spring cannot find my component?

like image 379
visst Avatar asked Mar 23 '15 22:03

visst


People also ask

Why Autowired is not working in Spring boot?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

Can we Autowire interface in Spring boot?

@Autowired in Spring Boot. In a Spring Boot application, auto-wiring is enabled by default.

Can we use @autowired for interface?

If you try to use @Autowired on an interface, the Spring framework would throw an exception as it won't be able to decide which implementation class to use.

Is @repository annotation needed?

It is indeed not necessary to put the @Repository annotation on interfaces that extend JpaRepository ; Spring recognises the repositories by the fact that they extend one of the predefined Repository interfaces.


1 Answers

When the repository package is different to @SpringBootApplication/@EnableAutoConfiguration, base package of @EnableJpaRepositories is required to be defined explicitly.

Try to add @EnableJpaRepositories("com.pharmacy.persistence.users.dao") to SpringBootRunner

like image 184
hang321 Avatar answered Sep 24 '22 07:09

hang321