【发布时间】:2017-01-26 18:15:00
【问题描述】:
jar(外部库)中有一些类在内部使用 Spring。 所以库类的结构如下:
@Component
public class TestBean {
@Autowired
private TestDependency dependency;
...
}
并且库提供了构造对象的API:
public class Library {
public static TestBean createBean() {
ApplicationContext context = new AnnotationConfigApplicationContext(springConfigs);
return context.getBean(TestBean);
}
}
在我的应用程序中,我有配置:
@Configuration
public class TestConfig {
@Bean
public TestBean bean() {
return Library.createBean();
}
}
抛出异常:Field dependency in TestBean required a bean of type TestDependency that could not be found.。
但是 Spring 不应该尝试注入一些东西,因为 bean 已经配置好了。
我可以为某个 bean 禁用 Spring 自动装配吗?
【问题讨论】:
-
你怎么知道bean已经配置好?
-
既然
Library.createBean()正在创建你的对象,你为什么要把@Autowired 放在TestDependency上面 -
不是我。这是一个库源代码。
-
@randominstanceOfLivingThing,因为
Library.createBean内部使用了 Spring。 -
可能是组件扫描类路径中找不到TestDependency?
标签: java spring spring-boot