英文:
How to have an `@Autowired` object in a `SpringBootContextLoader`?
问题
我有以下内容:
@Configuration
public class ConsorsCrucibleTestsConfiguration {
public AtomicReference<Functions.FailableFunction<MergedContextConfiguration, ApplicationContext, Exception>> crucibleApplicationContextFunction() {
return new AtomicReference<>(null);
}
public class SettableApplicationContextLoader extends SpringBootContextLoader {
public class SettableApplicationContextLoader extends SpringBootContextLoader {
@Autowired
private AtomicReference<FailableFunction<MergedContextConfiguration, ApplicationContext, Exception>> applicationContextFunction;
// 使用 applicationContextFunction
}
@SpringBootTest
@ContextConfiguration(loader = SettableApplicationContextLoader.class)
class CucumberStepsBase {
}
但当使用 `applicationContextFunction` 时,它为 `null`。是否有一种方法将其注入到上下文加载程序中?
英文:
I have the following:
@Configuration
public class ConsorsCrucibleTestsConfiguration {
public AtomicReference<Functions.FailableFunction<MergedContextConfiguration, ApplicationContext, Exception>> crucibleApplicationContextFunction() {
return new AtomicReference<>(null);
}
public class SettableApplicationContextLoader extends SpringBootContextLoader {
public class SettableApplicationContextLoader extends SpringBootContextLoader {
@Autowired
private AtomicReference<FailableFunction<MergedContextConfiguration, ApplicationContext, Exception>> applicationContextFunction;
// use `applicationContextFunction`
}
@SpringBootTest
@ContextConfiguration(loader = SettableApplicationContextLoader.class)
class CucumberStepsBase {
}
But when it's time to use applicationContextFunction
, it's null
. Is there a way to have it injected into the context loader?
答案1
得分: 1
上下文加载器的创建发生在应用程序中进行依赖注入之前。如果您使用断点,您会看到在通过SettableApplicationContextLoader
之前,您会进入该方法之前通过ConsorsCrucibleTestsConfiguration
bean方法。
您究竟想要实现什么?也许这可以是一个更好的开始。
英文:
A context loader creation happens before the dependency injection occurs in your application. If you use breakpoints, you will see that you pass into your SettableApplicationContextLoader
before passing through your ConsorsCrucibleTestsConfiguration
bean method.
What are you trying to achieve exactly ? Maybe this could be a better start.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论