JavaFX:将Spring框架与JavaFX应用程序集成(错误的配置)
作者:互联网
我正在开发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;
}
}
SpringLoader:
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配置.请让我知道.非常感谢.
标签:java,spring,javafx,javafx-8,configuration 来源: https://codeday.me/bug/20191010/1885001.html