我不断遇到问题标题中提到的问题。
我想用i.a.介绍一些DI
。 applicationContext.xml
文件和@ImportResource
批注。我已经检查了一些将applicationContext.xml
位置传递到@ImportResource
的方法
但它们不起作用。在关闭测试的情况下执行编译。我在项目和项目树中的某些文件的下面发布内容。您有什么想法可以解决这个问题吗?
GitHubDetailsApplication
package pl.cd.githubdetails;
// imports ...
@SpringBootApplication
// doesnt work:
// @ImportResource(locations = {"applicationContext.xml"})
// @ImportResource(locations = {"classpath:applicationContext.xml"})
// @ImportResource({"classpath*:applicationContext.xml"})
@ImportResource({"/applicationContext.xml"}) // also doesnt work
public class GitHubDetailsApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(GitHubDetailsApplication.class, args);
}
}
项目树
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pl.cd.githubdetails"/>
</beans>
GitController
package pl.cd.githubdetails;
// imports
@RestController
public class GitController {
@Autowired
GitHttpClient gitHttpClient;
// ...
}
GitHttpClient
package pl.cd.githubdetails;
public class GitHttpClient {
// imports ...
private RestTemplate restTemplate;
public GitHttpClient() {
this.restTemplate = new RestTemplate();
}
// ...
}
编辑。我插入堆栈。
2019-09-19 00:25:20.413 WARN 13584 --- [main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gitController': Unsatisfied dependency expressed through field 'gitHttpClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'pl.cd.githubdetails.GitHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-09-19 00:25:20.415 INFO 13584 --- [main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-09-19 00:25:20.425 INFO 13584 --- [main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-19 00:25:20.497 ERROR 13584 --- [main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field gitHttpClient in pl.cd.githubdetails.GitController required a bean of type 'pl.cd.githubdetails.GitHttpClient' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'pl.cd.githubdetails.GitHttpClient' in your configuration.
答案 0 :(得分:2)
您的控制器希望将注入类型为GitHttpClient
的bean:
@Autowired
GitHttpClient gitHttpClient;
但是没有这种豆。为什么?因为您尚未将类GitHttpClient
注释为Spring bean。你能做什么?将其注释为Service
或Component
:
@Service
public class GitHttpClient {
...
}