org.springframework.beans.factory.NoSuchBeanDefinitionException:@Autowired在Spring Boot中不起作用

时间:2017-11-01 13:07:12

标签: java spring spring-boot spring-data spring-data-jpa

我目前正在开发一个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)}

我目前的项目结构可以抽象为:

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")
}

构建此将导致上述错误,我不知道为什么。我不确定我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

尝试将MyApplicationcom.johndoe.myapp移至com.johndoe 您的MyApplication类应如下所示:

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的类:

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

Spring引导可以找到这个类并将其用于扫描(@SpringBootApplication注释包含@ComponentScan@ComponentScan的默认行为是查找同一个包中的类和所有子类 - 带注释的类的包)