我在这里明显遗漏了什么.我正在制作一个简单的spring boot
应用程序,spring data jpa
包含内容和面对错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [locassa.domain.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)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE] ... 32 common frames omitted
我的代码:
应用:
@SpringBootApplication @ComponentScan(basePackages = {"app.controller", "app.domain"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
的pom.xml
4.0.0 pl.mosek pl.mosek 0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.3.0.RELEASE org.springframework.boot spring-boot-starter-web org.projectlombok lombok 1.16.6 com.h2database h2 org.springframework.boot spring-boot-starter-data-jpa 1.8 org.springframework.boot spring-boot-maven-plugin
控制器:
@RestController public class TestController { @Autowired PersonService personService; @RequestMapping("/") public String index() { return "Test spring boot"; } @RequestMapping("/person/{id}") public Person personById(@PathVariable Long id) { return personService.findPerson(id); } }
PersonService:
public interface PersonService { Person findPerson(Long id); }
PersonServiceImpl:
@Service public class PersonServiceImpl implements PersonService { @Autowired PersonRepository personRepository; public Person findPerson(Long id) { return personRepository.findOne(id); } }
PersonRepository(这个不能自动装配):
public interface PersonRepository extends CrudRepository{ }
已在网上搜索过.我没找到一件事.有任何想法吗?
我也遇到了同样的问题.我用以下解决方案解决了这个问题.如果您的实体类和存储库位于不同的包中,则需要使用以下注释.
@SpringBootApplication
@EntityScan(basePackages = {"EntityPackage"} )
@EnableJpaRepositories(basePackages = {"RepositoryPackage"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}