我创建了一个spring boot应用程序,它使用spring数据postgresql来访问数据库。当我测试这个应用程序工作正常。但是,我想在不使用spring的其他项目中使用此应用程序。 我有一个包含两个四个java应用程序的大项目,我使用maven来集成所有这个项目(通过模块)。我以这种方式在java aplication中添加了依赖:
<dependency>
<groupId>databases</groupId>
<artifactId>databases</artifactId>
<version>${version}</version>
</dependency>
当我尝试使用在Spring项目中创建的类时,我遇到了使用@Autowired的问题,当我执行项目java时,出现空指针错误,因为注入的内部类是null。
这是在java项目中创建的类(不要使用spring boot)
public class MainController implements Initializable {
private TagServiceBean tagService = new TagServiceBean ();
@Override
public void initialize(URL location, ResourceBundle resources) {
Collection<Tag> tags = tagService.findAll();
for (Tag tag : tags){
selectedTags.getItems().add(tag.getF_tagname());
}
}
}
在“Collection tags = TagService.findAll();”行中出现以下错误:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at org.tenergia.historicosFX.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at org.tenergiaEditor.databases.service.spring.TagServiceBean.findAll(TagServiceBean.java:21)
at org.tenergia.historicosFX.controller.MainController.initialize(MainController.ja va:134)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
答案 0 :(得分:0)
我假设您在TagServiceBean
中使用@Autowired
将其他依赖项注入字段。由于您未在“Java项目”中使用Spring,因此这些字段不会自动填充值。
您可以手动填写所有这些字段(这将非常麻烦),或者在“Java项目”中使用Spring让Spring处于肮脏的工作中。