【发布时间】:2015-07-01 14:50:47
【问题描述】:
我正在开发一个 JavaFX 应用程序,我想将 Spring 功能与其集成。目前代码编译没有任何错误,但是当我请求标记为@Transactional 和@Service 的服务层方法时,我得到NullPointerException。我在 Spring 配置中做错了什么是我不明白的。这是我的 JavaFX 代码:
主类:
public class Main extends Application {
private static final SpringFxmlLoader loader = new SpringFxmlLoader();
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("login.fxml"));
stage.setTitle("APPNAME");
stage.setScene(new Scene(root, 300, 600));
stage.setFullScreen(false);
stage.setMaximized(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"packagename"})
public class ApplicationConfiguration {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
Properties properties = new Properties();
propertySourcesPlaceholderConfigurer.setProperties(properties);
return propertySourcesPlaceholderConfigurer;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("messages", "org.springframework.security.messages");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}
弹簧加载器:
public class SpringFxmlLoader {
private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
public Object load(String url) {
try (InputStream fxmlStream = SpringFxmlLoader.class
.getResourceAsStream(url)) {
System.err.println(SpringFxmlLoader.class
.getResourceAsStream(url));
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(new Callback<Class<?>, Object>() {
@Override
public Object call(Class<?> clazz) {
return applicationContext.getBean(clazz);
}
});
return loader.load(fxmlStream);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
}
}
}
现在在我的控制器中,我有这样的东西:
@Component
public class Controller implements Initializable {
@FXML
public TextField usernameField;
@FXML
public PasswordField passwordField;
@FXML
public Button submitButton;
@Autowired
private PersonService personService;
// Now the above personService throws me a NPE.
}
我是否以某种方式弄乱了 JavaFX 的 Spring 配置。请告诉我。非常感谢。 :-)
更新
在 James D.. 建议的更改之后,我收到以下错误:
null
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$2/1058634310.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
at tooltank.MainClass.SpringFxmlLoader.load(SpringFxmlLoader.java:28)
at tooltank.MainClass.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$57/667705538.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$53/767743416.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$55/1195477817.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$54/1403425489.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1429486634.run(Unknown Source)
... 1 more
Process finished with exit code 1
它发生在 SpringFXMLLoader.java 中的以下行:
return loader.load(fxmlStream);
【问题讨论】:
-
您的
start()方法没有使用您的SpringFxmlLoader.... -
@James_D :好的,你能告诉我怎么用吗?
-
嗯,当然,但是如果您没有看到问题,那么您似乎是在盲目地复制代码,而几乎不知道代码在做什么。我建议你真正弄清楚你的代码是做什么的。
-
@James_D :我明白,但我不是 JavafX 专家,这只是我正在运行的试验。此外,是否有像 Spring 那样的 JavaFX 文档,因为我在 Oracle 上检查的文档还不够好。
-
我同意
FXMLLoader文档确实很差,但很明显,由于您的SpringFxmlLoader创建了一个FXMLLoader并调用了它的load()方法,因此您应该使用它而不是自己创造一个,不是吗?
标签: java spring configuration javafx javafx-8