Search code examples
springjpaspring-boot

Spring Boot JPA : Autowired JPA repository is null


I'm programming a lib for the services / DAOs layer, trying to use Spring BOOT. I want to use JPA repositories, and will probably need to use JDBC as well.

So, I followed this tutorial, modifying it a bit but can't get it to work. The repository I want is not accessible through @Autowired annotation.

Here are my files, starting with pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.1.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

My main class :

@Configuration
@ComponentScan(basePackages = "politik.commons")
@EnableJpaRepositories
@EnableAutoConfiguration
public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Main.class, args);
    }

}

A bean :

@Entity
public class Article {

    @Column(name="hash")
    private String hash;

    @Column(name="url")
    private String url;

    @Column(name="url_source")
    private String urlSource;

    @Column(name="html")
    private String html;
}

The repository :

@Repository
public interface IArticleDAO extends JpaRepository<Article, Long> {
}

The test controller :

@RestController
public class TEstCtrl {

    @Autowired
    IArticleDAO articleDAO;

    public TEstCtrl () {
        System.out.println("coucou hiboux");
    }

    @RequestMapping("/")
    public String test() {
        String t = new String();
        for (Article article : articleDAO.findAll())
        {
            t = t + article.getUrl() + "\n";
        }
        return t;
    }
}

And finally, the stack trace :

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [politik.commons.dao.IArticleDAO] 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:1103)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 24 more

What am I missing ?

Thank you !


Solution

  • I think the problem is that your Article entity doesn't have a Long Id field. The second generic property in JpaRepository corresponds to the type of the id of the entity the repository manages. Change your Article class as follows and it should work:

    @Entity
    public class Article {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private long id;
    
        ...
    }