Java 通过字段'表示未满足的依赖关系;fooService';:没有为依赖项[aaa.FooService]找到类型为[aaa.FooService]的合格bean
我在Spring boot 1.4.0中遇到了这种奇怪的Spring行为。Spring基本上告诉我,它不能将bean自动连接到资源,因为它没有找到自己的依赖关系Java 通过字段'表示未满足的依赖关系;fooService';:没有为依赖项[aaa.FooService]找到类型为[aaa.FooService]的合格bean,java,spring,spring-mvc,spring-boot,dependency-injection,Java,Spring,Spring Mvc,Spring Boot,Dependency Injection,我在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 [*
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从测试中运行应用程序时,它也可以运行(所有内容,包括此资源)我今天设法找到了根本原因。它的Spring Boot开发工具——更确切地说是它的拆分类加载器(相关bug:) 当我在ListableBeanFactory中放置断点时,子REST@Configuration即将@Autowire该FooService 如果没有FooServiceInterface的FooService实例,则返回false。 当我做FooService.class.getClassLoader()和beanfactory.getBean(“FooService”/不能在这里使用类,会触发找不到异常/).getClass().getClassLoader()时,它们是不同的(一个是AppClassLoader,另一个是devtools可重启类加载器)
解决方案:从类路径中删除引导devtools。这样做的确切原因是,在运行时尝试将
FooService
自动连接到ServiceMocksRestConfig
时,Spring IOC容器中没有初始化类型为FooService
的bean
它可能是由开发中的不同错误引起的。这解决了每一个可能导致此问题的错误