Spring Boot JPA:自动装配的 JPA 存储库为空

问题描述 投票:0回答:2

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

因此,我按照本教程进行了一些修改,但无法使其工作。我想要的存储库无法通过

@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

我错过了什么?

谢谢你!

spring jpa spring-boot
2个回答
3
投票

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

@Entity
public class Article {

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

    ...
}

0
投票

我也面临着类似的问题。

[enter image description here][1]
[enter image description here][2]
[enter image description here][3]
[enter image description here][4]
[1]: https://i.stack.imgur.com/0L0LM.png
[2]: https://i.stack.imgur.com/CiR18.png
[3]: https://i.stack.imgur.com/W0DDB.png
[4]: https://i.stack.imgur.com/1V2iY.png
© www.soinside.com 2019 - 2024. All rights reserved.