我正在编写一个使用spring进行依赖注入的应用程序,并使用javafx进行gui。在spring config中我定义了所有的bean(DAO,服务和GUI控制器)。一切都很好。但是我现在遇到了一些麻烦。
在大型机控制器中,我使用了一个名为toolbox的服务。它被注入,我可以使用它就好了。但是当我想打开第二个窗口时,它也使用相同的服务,它似乎没有被注入。我一直在获得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控制器中,我为它定义了变量和一个setter。
如果我从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 :(得分:0)
从您的配置中,您可以看到您只将工具箱服务注入TagListFrame
类对象:
<bean id="TagListFrame" name="TagListFrame" class="TagListFrame" scope="singleton">
<property name="toolBox" ref="ToolBoxService"/>
</bean>
如果要将相同的服务注入其他控制器,则必须在xml中以与TagListFrame相同的方式指定此注入。像这样的Smth
<bean id="MyNewController" name="MyNewController" class="MyNewController" scope="singleton">
<property name="toolBox" ref="ToolBoxService"/>
</bean>
P.S。 Singleton是默认范围,因此您无需明确指定它。