현재 스프링 부트 애플리케이션에서 작업 중이며 @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
를 이동
'@ SpringBootApplication'에서'scanBasePackages' 속성을 제거하고 어떤 일이 일어나는지 봅니다. – Antoniossss
'MyDataRepository'에서'@ Repository'를 제거하십시오. –
@Antoniossss 당신의 제안을 시도했지만 여전히 같은 오류가 발생합니다. – MiG98789