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
cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Spring 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 ComponentScan
scans only com.oni.controller
and com.oni.service
packages to inject the dependencies, so change it to scan through the whole "com.oni" package as shown below:
您ComponentScan
只扫描com.oni.controller
和com.oni.service
包以注入依赖项,因此将其更改为扫描整个“com.oni”包,如下所示:
@ComponentScan(basePackages = "com.oni")
or the other option is include com.oni.excelReader
package (in which your ReadSheet
class 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 Application
class to com.oni
, you don't need @ComponentScan
at all, Spring boot automatically scans all sub-packages of com.oni
for you.
PS:如果你移动你的Application
类com.oni
,你并不需要做@ComponentScan
的一切,春天开机自动扫描所有的子包com.oni
为您服务。