I'm currently developing an application with spring boot and JavaFX and I'm experiencing some difficulties with @Value annotated properties.
I've already tried specifying @PropertySource, introducing PropertyPlaceholderConfigurer and defining scanBasePackages to @SpringBootApplication annotation.
My project's files looks like this:
Configuration class
@Configuration
@ComponentScan(basePackages = "pl.baadamczyk.workflowenh")
public class ApplicationConfiguration {
}
Main class
@SpringBootApplication(scanBasePackages = { "pl.baadamczyk" })
public class WFEApplication extends Application {
private ConfigurableApplicationContext springContext;
private Parent root;
@Value("${language}")
public String localeProperty; // this one resolves to null
@Autowired
public PropertySourcesPlaceholderConfigurer configurer;
@Override
public void init() throws Exception {
springContext = SpringApplication.run(WFEApplication.class);
}
@Override
public void start(Stage primaryStage) throws Exception {
setupLoader();
setupPrimaryStage(primaryStage);
}
@Override
public void stop() throws Exception {
springContext.stop();
}
private void setupLoader() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/MainView.fxml"));
fxmlLoader.setControllerFactory(springContext::getBean);
fxmlLoader.setResources(getResourcesForCurrentLocale());
root = fxmlLoader.load();
}
private ResourceBundle getResourcesForCurrentLocale() {
Locale currentLocale = new Locale("EN");
return ResourceBundle.getBundle("translations", currentLocale);
}
private void setupPrimaryStage(Stage primaryStage) {
Scene scene = new Scene(root, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(WFEApplication.class, args);
}
application.properties contains only this one line:
language=EN