복붙노트

[SPRING] Spring @Autowired는 Jpa 저장소를 연결할 수 없습니다.

SPRING

Spring @Autowired는 Jpa 저장소를 연결할 수 없습니다.

분명히 여기에 뭔가 빠져 있습니다. 나는 spring data jpa inluded와 face follwing error로 간단한 spring boot app를 만들고있다.

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.hml

<?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>

제어 장치:

@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 (이 하나 autowired 수 없습니다) :

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

이미 웹에서 검색되었습니다. 나는 물건을 찾지 못했습니다. 어떤 아이디어?

해결법

  1. ==============================

    1.나는 또한 동일한 문제를 가지고 있었다. 나는 해결책을 따라 그것을 해결했다. 엔티티 클래스와 저장소가 다른 패키지에 있으면 다음 주석을 사용해야합니다.

    나는 또한 동일한 문제를 가지고 있었다. 나는 해결책을 따라 그것을 해결했다. 엔티티 클래스와 저장소가 다른 패키지에 있으면 다음 주석을 사용해야합니다.

    @SpringBootApplication
    @EntityScan(basePackages = {"EntityPackage"} )
    @EnableJpaRepositories(basePackages = {"RepositoryPackage"})
    public class Application {
    
        public static void main(String[] args) {
    
            SpringApplication.run(Application.class, args);
    
        }
    }
    
  2. from https://stackoverflow.com/questions/34169321/spring-autowired-can-not-wire-jpa-repository by cc-by-sa and MIT license