![](/img/trans.png)
[英]RabbitMQ Spring boot org.springframework.beans.factory.NoSuchBeanDefinitionException
[英]org.springframework.beans.factory.NoSuchBeanDefinitionException: @Autowired not working in Spring Boot
我目前正在使用Spring Boot應用程序,而@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)}
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中:
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);
}
}
在MyData.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
}
在MyDataRepository.java中:
package com.johndoe.repos;
import com.johndoe.data.MyData;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.*;
@Repository
public interface MyDataRepository extends CrudRepository<Tour, Long> {
List<MyData> findAll();
// More functions
}
在MyRequest.java中:
package com.johndoe.request;
import org.springframework.stereotype.Component;
@Component
public class MyRequest {
// More code
}
在MyHandler.java中:
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
}
在build.gradle中:
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")
}
構建此文件將導致上述錯誤,但我不確定為什么。 我不確定我在做什么錯。
嘗試將MyApplication
從com.johndoe.myapp
移至com.johndoe
類應類似於:
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
中沒有其他帶有注解@SpringBootApplication
類:
compile project(':<other folder in MyProject>')
春天開機可以找到這個類,並用它來掃描( @SpringBootApplication
注釋包含@ComponentScan
。的默認行為@ComponentScan
是相同的封裝和注解類的所有子包中尋找類)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.