在Spring Boot中无法解析@Value注释的属性

时间:2018-11-20 22:20:44

标签: java spring spring-boot properties-file

我目前正在使用Spring Boot和JavaFX开发应用程序,并且在使用@Value注释属性时遇到了一些困难。

我已经尝试过指定@PropertySource,引入PropertyPlaceholderConfigurer并为@SpringBootApplication注释定义scanBasePackages。

我项目的文件如下:

配置类

@Configuration
@ComponentScan(basePackages = "pl.baadamczyk.workflowenh")
public class ApplicationConfiguration {

}

主班

@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仅包含这一行:

language=EN

0 个答案:

没有答案