【发布时间】:2016-12-08 18:22:37
【问题描述】:
我在 Spring boot 1.4.0 中遇到过这种 Spring 的奇怪行为。 Spring 基本上告诉我它不能将 bean 自动装配到资源,因为它没有发现自己的依赖关系。
UnsatisfiedDependencyException: Error creating bean with name 'restResource': Unsatisfied dependency expressed through field
'fooService': No qualifying bean of type [**aaa.FooService**] found for dependency [**aaa.FooService**]
FooService 在资源中自动装配。当我将它@Autowire 到创建资源的@Configuration 文件中时,它会按预期注入那里。
这行得通:
public class ServiceMocksRestConfig extends WebMvcConfigurerAdapter {
@Autowired
private FooService fooService; //instance here
@Bean
public FooResource fooResource() {
return new FooResource(); // debuger stop here
}
//调试器步入
@RestController
public class FooResource {
@Autowired
private FooService fooService; //bang
有人知道,可能出了什么问题吗?
有趣的是,当我使用 boot spring runner 从测试中运行应用程序时,它也可以工作(一切,包括这个资源)
【问题讨论】:
标签: java spring spring-mvc spring-boot dependency-injection