Evaluating spring @value annotation as primitive boolean

后端 未结 4 728
情歌与酒
情歌与酒 2020-12-05 17:47

I have a spring @configuration annotated class MappingsClientConfig with a boolean field as:

 @Value(\"${mappings.enabled:true}\")
    private boolean mappi         


        
4条回答
  •  再見小時候
    2020-12-05 18:42

    Looks like you're missing the PropertyPlaceholderConfigurer. You need to register it as a bean factory post processor. Theoretically this could be done like this:

    public class PostProcessorConfig {
    
        @Bean
        public BeanFactoryPostProcessor getPP() {
           PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
           configurer.setLocations(new Resource[]{new ClassPathResource("/my.properties")});
           return configurer;
        }
    }
    

    However, there seems to be a bug that causes other issues doing this from a java based configuration. See ticket for workarounds.

提交回复
热议问题