java 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43134489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:07:22  来源:igfitidea点击:

cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Spring Boot

javaspringexceptionspring-boot

提问by r.kothari

I am working on a spring boot application. I want to achieve sending some json data to a web api. When I try to run it, the following error comes up. After so many attempts, its not getting resolved. Any help will be much appreciated:

我正在开发一个 Spring Boot 应用程序。我想实现将一些 json 数据发送到 web api。当我尝试运行它时,出现以下错误。经过这么多次尝试,它没有得到解决。任何帮助都感激不尽:

Error console:

错误控制台:

2017-03-31 12:00:36.634  WARN 2972 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oniSavingsApiController': Unsatisfied dependency expressed through field 'oniSavingsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oniSavingsApiService': Unsatisfied dependency expressed through field 'readSheet'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.oni.excelReader.ReadSheet' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-03-31 12:00:36.634  INFO 2972 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-31 12:00:36.654  INFO 2972 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-31 12:00:36.798 ERROR 2972 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

APPLICATION FAILED TO START

Description:

Field readSheet in com.oni.service.OniSavingsApiService required a bean of type 'com.oni.excelReader.ReadSheet' that could not be found.


Application

应用

@SpringBootApplication
@ComponentScan(basePackages = "com.oni.controller, com.oni.service")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Controller

控制器

@Controller
public class OniSavingsApiController {

    @Autowired
    private OniSavingsApiService oniSavingsService;

    @RequestMapping("/")
    public void home() {
        oniSavingsService.oakRestCall();
    }
}

Service

服务

@Component
public class OniSavingsApiService {

@Autowired
private ReadSheet readSheet;

public static final String CUSTOM_INFO = "custominformation";
public static final String AUTHORIZATION = "Basic";

public void oakRestCall() {

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", AUTHORIZATION);

    ArrayList<ResponseEntity<ResponseData>> responses = new ArrayList<ResponseEntity<ResponseData>>();
    List<ExcelFields> listOfExcelData = readSheet.getFileContent();

    for (ExcelFields ef : listOfExcelData) {
        System.out.println(ef.getId());
        try {
            String json = "";
            String urlString = "";
            /**
            some code here
            **/
        } catch (Exception e) {
        }
    }
}
}

采纳答案by developer

Your ComponentScanscans only com.oni.controllerand com.oni.servicepackages to inject the dependencies, so change it to scan through the whole "com.oni" package as shown below:

ComponentScan只扫描com.oni.controllercom.oni.service包以注入依赖项,因此将其更改为扫描整个“com.oni”包,如下所示:

@ComponentScan(basePackages = "com.oni")

or the other option is include com.oni.excelReaderpackage (in which your ReadSheetclass is located) as well:

或者另一个选项也是包含com.oni.excelReader包(您的ReadSheet类所在的包):

@ComponentScan(basePackages = "com.oni.controller, 
                           com.oni.service, com.oni.excelReader")

P.S.:If you move your Applicationclass to com.oni, you don't need @ComponentScanat all, Spring boot automatically scans all sub-packages of com.onifor you.

PS:如果你移动你的Applicationcom.oni,你并不需要做@ComponentScan的一切春天开机自动扫描所有的子包com.oni为您服务。