【发布时间】:2017-12-09 09:52:28
【问题描述】:
假设我有一个 JavaFx 类和一个 ViewState 类,它们需要在 start 方法中创建 stage 的引用。我如何能够自动装配这种依赖关系?我知道 Stage.class 没有被注释为 @Component 所以 Spring 无法检测到 duch @Bean。
@SpringBootApplication
@ComponentScan({"controller","service","dao","javafx.stage.Stage"})
@EntityScan( basePackages = {"Model"})
@Import({ SpringConfig.class})
public class JavaFXandSpringBootTestApplication extends Application{
private ApplicationContext ctx;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws Exception {
ViewState viewState = ctx.getBean(ViewState.class);
}
ViewState 类:
@Componenet
public class ViewState {
@Autowired
private ApplicationContext ctx;
private Stage stage;
@Autowired
public ViewState(Stage stage)
{
this.stage = stage;
}
}
编译器按摩:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javafx.stage.Stage' available: expected at least 1
【问题讨论】: