2017-11-01 1 views
0

현재 스프링 부트 애플리케이션에서 작업 중이며 @Autowired은 테스트 클래스 인 메인 클래스에는 작동하지 않습니다. 나는 이미 다른 해결책을 시도해 왔지만 지금까지 아무 것도 나를 위해 일해오고 있지 않다. 내가 할org.springframework.beans.factory.NoSuchBeanDefinitionException : @Autowired가 스프링 부트에서 작동하지 않습니다.

오류는

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.johndoe.handler.MyHandler' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.johndoe.repos.MyDataRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 있습니다.

나의 현재 프로젝트 구조는이에 추상화 될 수 있습니다

MyProject 
|- Some other folders and files 
|- MyApplicationFolder 
    |- build.gradle 
    |- bin 
    |- build 
    |- src 
     |- main 
     |- java 
      |- com 
       |- johndoe 
        |- repos 
        |- MyDataRepository.java 
        |- data 
        |- MyData.java 
        |- myapp 
        |- MyApplication.java 
        |- handler 
        |- MyHandler.java 
        |- request 
        |- MyRequest.java 
     |- resources 
     |- test 

MyApplication.java에서 : MyData.java에서

package com.johndoe.myapp; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 

@SpringBootApplication(scanBasePackages = {"com.johndoe", "com.johndoe.repos", "com.johndoe.data", "com.johndoe.myapp", "com.johndoe.handler", "com.johndoe.request"}) 
@EnableJpaRepositories("com.johndoe.repos") 
public class MyApplication { 
    public static void main(String[] args) {   
     SpringApplication.run(MyApplication.class, args); 
    } 
} 

: MyDataRepository.java에서

package com.johndoe.data; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Data 
@Entity 
@Table(name = "my_table") 
public class MyData { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private String id; 

    // More data members 
} 

:

MyRequest.java에서

:

apply plugin: 'org.springframework.boot' 

dependencies { 
    compile project(':<other folder in MyProject>') 
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE") 
    compile("org.springframework.boot:spring-boot-starter-test") 
    compile("org.hsqldb:hsqldb:2.3.0") 
    compile("org.springframework.boot:spring-boot-starter-web") { 
     exclude module: "spring-boot-starter-tomcat" 
    } 
    compile("org.springframework.boot:spring-boot-autoconfigure") 
} 

상기 오류 및 I가 발생합니다이 건물 : build.gradle에서

package com.johndoe.handler; 

import com.johndoe.request.MyRequest; 
import com.johndoe.data.MyData; 
import com.johndoe.repos.MyDataRepository; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component 
public class MyHandler { 
    @Autowired 
    private MyDataRepository repo; 
    @Autowired 
    private MyRequest req; 

    // More code 
} 

: MyHandler.java에서

package com.johndoe.request; 

import org.springframework.stereotype.Component; 

@Component 
public class MyRequest { 
    // More code 
} 

이유를 모르겠다. 내가 여기서 잘못하고있는 것이 확실하지 않습니다.

compile project(':<other folder in MyProject>') 

:

package com.johndoe; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

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

당신이 당신의 의존성에 주석 @SpringBootApplication와 다른 클래스가없는 있는지 확인하십시오 com.johndoe.myapp com.johndoe에 대한 귀하의에서는 MyApplication 클래스에서 MyApplication를 이동

+0

'@ SpringBootApplication'에서'scanBasePackages' 속성을 제거하고 어떤 일이 일어나는지 봅니다. – Antoniossss

+0

'MyDataRepository'에서'@ Repository'를 제거하십시오. –

+0

@Antoniossss 당신의 제안을 시도했지만 여전히 같은 오류가 발생합니다. – MiG98789

답변

0

봅니다 같은 것이 보일 것입니다 스프링 부트는이 클래스를 찾아 스캔을 위해 사용할 수 있습니다 (@SpringBootApplication 주석은 @ComponentScan을 포함합니다.) 기본 동작 @ComponentScan은 동일한 패키지와 주석이 달린 클래스의 모든 하위 패키지에서 클래스를 찾습니다.)

+0

왜 나는 왜 스캔 패키지가 선언 된 패키지로 작동하지 않는지 궁금합니다. – Antoniossss

+0

'@ SpringBootApplication'의'scanBasePackages'가 충분하지 않다는 것을 이해합니다. 하지만을 추가해야합니다. @EntityScan (basePackages = { "com.johndoe.data"})' 그러나 쉬운 수정은'MyApplication' – sergey

+0

'@ EntityScan' reffers를 도메인 클래스 ('@Entitiy ') Spring 컴포넌트가 아니다. – Antoniossss

관련 문제