如何在`SpringBootContextLoader`中拥有一个`@Autowired`对象?

huangapple go评论136阅读模式
英文:

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&lt;Functions.FailableFunction&lt;MergedContextConfiguration, ApplicationContext, Exception&gt;&gt; crucibleApplicationContextFunction() {
    return new AtomicReference&lt;&gt;(null);
  }
public class SettableApplicationContextLoader extends SpringBootContextLoader {
  public class SettableApplicationContextLoader extends SpringBootContextLoader {
  @Autowired
  private AtomicReference&lt;FailableFunction&lt;MergedContextConfiguration, ApplicationContext, Exception&gt;&gt; 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.

huangapple
  • 本文由 发表于 2020年1月7日 02:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定