简体   繁体   English

Spring @Autowired无法连线Jpa存放区

[英]Spring @Autowired can not wire Jpa repository

I clearly missing something here. 我显然在这里错过了一些东西。 I'm making a simple spring boot app with spring data jpa inluded and face follwing error: 我正在制作一个包含spring data jpa和出现以下错误的简单spring boot应用程序:

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

My code: 我的代码:

Application: 应用:

@SpringBootApplication
@ComponentScan(basePackages = {"app.controller", "app.domain"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.mosek</groupId>
    <artifactId>pl.mosek</artifactId>
    <version>0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Controller: 控制器:

@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: PersonServiceImpl:

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonRepository personRepository;

    public Person findPerson(Long id) {
       return personRepository.findOne(id);
    }
}

PersonRepository (this one cannot be autowired): PersonRepository(无法自动连线):

public interface PersonRepository extends CrudRepository<Person, Long> {
}

Searched on the web already. 已在网上搜索。 I didnt found a thing. 我没发现一件事。 Any ideas? 有任何想法吗?

I also had the same problem.I solved it with following solution. 我也有同样的问题,我通过以下解决方案解决了。 If your Entity classes and Repositories in a different package you need to use following annotations. 如果您的实体类和存储库位于不同的包中,则需要使用以下注释。

@SpringBootApplication
@EntityScan(basePackages = {"EntityPackage"} )
@EnableJpaRepositories(basePackages = {"RepositoryPackage"})
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot @Autowired Jpa存储库返回Null - Spring Boot @Autowired Jpa repository returns Null 如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 spring 服务中 - How to implement Generic JPA Repository in Spring Boot - Which can be autowired into spring services for any entity/class type 无法在 Spring Boot 应用程序的 Main 方法中使用 @Autowired JPA Repository - Can't use @Autowired JPA Repository in Main method of Spring Boot application Spring数据jpa存储库注入失败,在Spring启动时使用@Autowired - spring data jpa repository inject fails using @Autowired in spring boot 如何在不自动接线的情况下实例化Spring Data JPA存储库 - how to instantiate spring data jpa repository without autowired @Autowired不会注入Spring Data JPA存储库-NullPointerException - @Autowired does not inject Spring Data JPA Repository - NullPointerException 为什么我的Spring Boot自动连接的JPA存储库未通过JUnit测试? - Why is my Spring Boot autowired JPA Repository failing JUnit test? Spring Boot应用程序中的JUnit测试中没有自动装配JPA存储库的合格Bean - No qualifying bean for autowired JPA repository in JUnit test in Spring Boot application 我可以在 Spring boot 自定义验证器中自动连接一个存储库吗 - Can I Autowired one Repository inside Spring boot custom validator Spring Autowired存储库中的抽象类 - Spring Autowired Repository in Abstract Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM