我在这里明显遗漏了什么.
我正在制作一个简单的弹簧启动应用程序,其中包含弹簧数据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
控制器:
@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
已在网上搜索过.我没找到一件事.有任何想法吗?
最佳答案
我也遇到了同样的问题.我用以下解决方案解决了这个问题.如果您的实体类和存储库位于不同的包中,则需要使用以下注释.
原文链接:https://www.f2er.com/spring/432014.html@SpringBootApplication
@EntityScan(basePackages = {"EntityPackage"} )
@EnableJpaRepositories(basePackages = {"RepositoryPackage"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}