微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring @Autowired无法连接Jpa存储库

在这里明显遗漏了什么.
我正在制作一个简单的弹簧启动应用程序,其中包含弹簧数据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

spring-boot-starter-parentspring-boot-starter-webspring-boot-starter-data-jpa

控制器:

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

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐