Spring Boot JPA:Autowired JPA存储库为null

时间:2015-04-14 14:00:57

标签: spring jpa spring-boot

我正在为服务/ DAO层编写一个lib,试图使用Spring BOOT。我想使用JPA存储库,也可能需要使用JDBC。

所以,我跟着this tutorial,对它进行了一些修改,但无法使其正常工作。我想要的存储库无法通过@Autowired注释访问。

以下是我的文件,以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>

我的主要课程:

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

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

}

豆子:

@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;
}

存储库:

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

测试控制器:

@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;
    }
}

最后,堆栈跟踪:

 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

我错过了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为问题在于您的文章实体没有Long Id字段。 JpaRepository中的第二个通用属性对应于存储库管理的实体的id的类型。如下更改您的文章类,它应该工作:

@Entity
public class Article {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    ...
}