0
我正在使用JavaFX 2與Spring框架一起使用,但是注入發生的很晚。我的控制器被FXML-Loader實例化,Spring控制器的成員變量注入工作,但它工作得太晚了,這意味着在(1)注入中沒有發生,而在(2)注入的確發生了:依賴注入太遲
public class MainController extends AbstractController
{
@Autowired
public StatusBarController statusbarController;
// Implementing Initializable Interface no longer required according to
// http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm:
private void initialize() {
BorderPane borderPane = (BorderPane)getView();
borderPane.setBottom(statusbarController.getView()); // (1) null exception!
}
// Linked to a button in the view
public void sayHello() {
BorderPane borderPane = (BorderPane)getView();
borderPane.setBottom(statusbarController.getView()); // (2) works!
}
}
任何方式讓Spring在以前的狀態注入statusbarController
?我不能讓用戶必須點擊一個按鈕來加載我的GUI ;-)
我AppFactory是這樣的:
@Configuration
public class AppFactory
{
@Bean
public MainController mainController() throws IOException
{
return (MainController) loadController("/main.fxml");
}
protected Object loadController(String url) throws IOException
{
InputStream fxmlStream = null;
try
{
fxmlStream = getClass().getResourceAsStream(url);
FXMLLoader loader = new FXMLLoader();
Node view = (Node) loader.load(fxmlStream);
AbstractController controller = (AbstractController) loader.getController();
controller.setView(view);
return controller;
}
finally
{
if (fxmlStream != null)
{
fxmlStream.close();
}
}
}
}
謝謝,這解決了問題!記錄:有一個例子,例如這裏http://koenserneels.blogspot.de/2012/11/javafx-2-with-spring.html(搜索setcontrollerfactory) – 2013-02-12 15:17:00