0

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
  • 1
    Autowired instead of Value, but the same problem: You have two different instances of your class. In this case, perhaps use a `CommandLineRunner` to do your launch with a constructed instance? – chrylis -cautiouslyoptimistic- Nov 20 '18 at 22:24
  • I've managed to solve the issue. It was just like @chrylis suggested. There were actually two instances of 'Main' object. I've fixed this one by adding following line to overrided 'init' method. **springContext.getAutowireCapableBeanFactory().autowireBean(this);** Thanks! – Bartłomiej Adamczyk Nov 20 '18 at 23:27

0 Answers0