【发布时间】:2016-04-29 17:49:06
【问题描述】:
我是春季菜鸟,我正在为此苦苦挣扎。
基本上,在开始使用 Spring 和 JPA 开发我的服务器之前,我尝试开始一个简单的示例,只是为了习惯这个框架。 我已经成功地使 Spring 与 Log4J、Swagger 等一些框架一起工作。现在我正在尝试使用 JPA,并且有一些点我可以找到解决方案。
我看到了一些关于如何使用它进行开发的博客,并从我选择创建我的 Repository Interfece 和extend Repository<T, ID> 的所有数以千计的选项中。你可以在下面看到我的代码:
package com.example.model;
@Entity
public class Person {
@Id
public Integer id;
public String name;
public Person(){}
}
package com.example.repository;
public interface PersonRepository extends Repository<Person, Integer> {
Collection<Person> findAll();
}
package com.example.controller;
@RestController
public class PersonController {
@Autowired
private PersonRepository repo;
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public Collection<Person> getAll() {
return repo.findAll();
}
}
package com.example;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我还有 application.properties 文件:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/test_db
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
当我让服务器运行时,我得到以下异常:
: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
: Closing JPA EntityManagerFactory for persistence unit 'default'
: Stopping service Tomcat
: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] 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)}
我创建了一个 Github 存储库来分享代码 here。
任何关于我做错了什么的线索?
【问题讨论】:
-
你的课程包是什么?只有在与 App 类位于相同或子包中时,才会自动检测您的存储库。
-
@dunni 我删除只是为了简化,但它们是:package com.example.controllers;包 com.example.entities;包 com.example.repository;包 com.example;
-
假设您拥有所有依赖项。在您的 App 类中,您可以将其放在@SpringBootApplication 下方:@ComponentScan(basePackages="com.example")。然后再试一次。
-
@KennyTaiHuynh SpringBootApplication ComponentScan(basePackages="com.example") public class App { Same exeption :S
-
我的意思是在@SpringBootApplication 行下方。在另一行中,将 @ComponentScan(basePackages="com.example").
标签: java spring spring-data spring-data-jpa