2013-12-13 91 views
0

我正在寫一個應用程序,使用彈簧的依賴注入和gui的javafx。在春季配置中,我定義了所有的bean(DAO,服務和GUI控制器)。一切都很好。但是我現在遇到了一些麻煩。春天不注入singleton bean控制器到另一個控制器

在主機控制器中,我使用了一個叫做工具箱的服務。它被注入,我可以使用它很好。然而,當我想打開第二個窗口,它也使用相同的服務,它似乎並沒有被注入。我不斷收到nullpointerexceptions,告訴我toolBox爲null。

我的彈簧配置爲:

<bean id="TagListFrame" name="TagListFrame" class="TagListFrame" scope="singleton"> 
    <property name="toolBox" ref="ToolBoxService"/> 
</bean> 
<bean id="ToolBoxService" name="ToolBoxService" class="ToolBoxImpl" scope="singleton"> 
    <property name="tagDao" ref="JDBCTagDao"/> 
</bean> 
<bean id="JDBCTagDao" name="JDBCTagDao" class="JDBCTagDao" scope="singleton"> 
    <property name="connection" ref="HSQLDBConnection"/> 
</bean> 
在TagListFrame控制器

我定義的變量和用於它的裝定。

如果我從mainframecontroller調用setter函數並使用它對工具箱的引用,它工作正常,但我似乎無法讓它與spring一起工作。

我打開新的窗口,用下面的代碼:

Scene newScene = new Scene(root, 400, 300); 
Stage newStage = new Stage(); 
newStage.setTitle("Window Title); 
newStage.initOwner(primaryStage); 
newStage.setScene(newScene); 
newStage.setX(primaryStage.getX() + 100); 
newStage.setY(primaryStage.getY() + 100); 
newStage.show(); 

堆棧跟蹤:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException 
at at.ac.tuwien.sepm.ws13.qse01.service.PictureInfoShowImpl.getAllTags(PictureInfoShowImpl.java:226) 
at at.ac.tuwien.sepm.ws13.qse01.gui.SinglePictureFrameController.showTags(SinglePictureFrameController.java:243) 
at at.ac.tuwien.sepm.ws13.qse01.gui.SinglePictureFrameController.setPicture(SinglePictureFrameController.java:196) 
at at.ac.tuwien.sepm.ws13.qse01.gui.MainFrameController.viewSinglePicture(MainFrameController.java:382) 
at at.ac.tuwien.sepm.ws13.qse01.gui.ThumbnailFrameController.openSingleImage(ThumbnailFrameController.java:281) 
at at.ac.tuwien.sepm.ws13.qse01.gui.ThumbnailFrameController.access$600(ThumbnailFrameController.java:37) 
at at.ac.tuwien.sepm.ws13.qse01.gui.ThumbnailFrameController$2.handle(ThumbnailFrameController.java:251) 
at at.ac.tuwien.sepm.ws13.qse01.gui.ThumbnailFrameController$2.handle(ThumbnailFrameController.java:226) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33) 
at javafx.event.Event.fireEvent(Event.java:171) 
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3100) 
at javafx.scene.Scene$ClickGenerator.access$8600(Scene.java:3038) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3320) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151) 
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:530) 
at com.sun.glass.ui.View.notifyMouse(View.java:924) 
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89) 
at java.lang.Thread.run(Thread.java:744) 

我會非常感謝任何幫助,謝謝!

+0

你可以發佈你創建第二個窗口的代碼嗎?我不知道你這樣做,但你需要有春天g創建你的豆autowire字段 –

回答

0

從你的配置,你可以看到你注入工具箱服務只爲TagListFrame類對象:

<bean id="TagListFrame" name="TagListFrame" class="TagListFrame" scope="singleton"> 
    <property name="toolBox" ref="ToolBoxService"/> 
</bean> 

如果要注入相同的服務爲其他控制器則必須指定此注射在XML相同您爲TagListFrame所做的一切。像這樣的

<bean id="MyNewController" name="MyNewController" class="MyNewController" scope="singleton"> 
    <property name="toolBox" ref="ToolBoxService"/> 
</bean> 

P.S. Singleton是默認範圍,因此您不需要明確指定它。

+0

感謝tipp,但那已經是我正在做的。這只是Spring配置的一部分。 – dominik

+0

,並確保你沒有使用關鍵字new創建服務,因爲它必須是Spring託管bean。如果您添加了完整的異常堆棧跟蹤將會很有幫助 – jonasnas

相關問題