gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:49 28 4
gpt4 key购买 nike

我正在开发一个 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

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

最佳答案

你的 ComponentScan 只扫描 com.oni.controllercom.oni.service 包来注入(inject)依赖,所以改成扫描通过整个“com.oni”包,如下所示:

@ComponentScan(basePackages = "com.oni")

或者另一个选项是包括 com.oni.excelReader 包(您的 ReadSheet 类位于其中):

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

P.S.:如果您Application类移动到com.oni,则不需要@ComponentScan 完全Spring boot 会自动为你扫描com.oni 的所有子包。

关于java - 取消刷新尝试 : org. springframework.beans.factory.UnsatisfiedDependencyException: Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134489/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com