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

时间:2017-03-31 07:05:01

标签: java spring exception spring-boot

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

错误控制台:

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.

应用

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

控制器

@Controller
public class OniSavingsApiController {

    @Autowired
    private OniSavingsApiService oniSavingsService;

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

服务

@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) {
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

您的ComponentScan仅扫描com.oni.controllercom.oni.service个软件包以注入依赖项,因此请更改它以扫描整个“com.oni”软件包,如下所示:

@ComponentScan(basePackages = "com.oni")

或其他选项包括com.oni.excelReader包(您的ReadSheet类所在的包):

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

PS:如果您Application课程移至com.oni,则根本不需要@ComponentScan ,< strong> Spring启动会自动扫描com.oni 的所有子包。