Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalArgumentException: Invalid boolean value when injecting property to boolean in Spring

In Spring Boot I attempt to inject a boolean value to an instance variable from an enviroment property which is set (enabled=true).

@Value("${enabled}")
private boolean enabled;

However Spring cannot resolve this for some reason and reports:

Caused by: java.lang.IllegalArgumentException: Invalid boolean value [${enabled}]

It seems like it does not replace expression ${enabled} with the property value.

What needs to be set ? Why doesn't work this by default ?

like image 885
ps-aux Avatar asked Oct 30 '22 18:10

ps-aux


2 Answers

For Spring-Boot you can find good reference here: http://www.baeldung.com/properties-with-spring

For given someprops.properites file:

somevalue:true

Here is FAILING version:

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource( "/someprops.properties" )
public class FailingNg {

    @Configuration
    public static class Config {
    }

    @Value("${somevalue}")
    private Boolean somevalue;

    // Fails with context initializatoin java.lang.IllegalArgumentException: Invalid boolean value [${somevalue}]
    @Test
    public void test1() {
        System.out.println("somevalue: " + somevalue);
    }
}

Here is WORKING version:

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class WorkingOk {

    @Configuration
    public static class Config {
        @Bean
        public static PropertyPlaceholderConfigurer properties() {
            PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
            Resource[] resources = new ClassPathResource[] { 
                    new ClassPathResource( "someprops.properties" )
            };
            ppc.setLocations( resources );
            ppc.setIgnoreUnresolvablePlaceholders( true );
            return ppc;
        }
    }

    @Value("${somevalue}")
    private Boolean somevalue;

    @Test
    public void test1() {
        System.out.println("somevalue: " + somevalue);
    }
}
like image 131
Witold Kaczurba Avatar answered Nov 02 '22 10:11

Witold Kaczurba


As mentioned in the comments, you are likely missing a PropertySourcesPlaceholderConfigurer bean definition. Here's an example of how to configure that within Java:

@Configuration
public class PropertyConfig {

    private static final String PROPERTY_FILENAME = "app.properties"; // Change as needed.    

   /**
     * This instance is necessary for Spring to load up the property file and allow access to
     * it through the @Value(${propertyName}) annotation. Also note that this bean must be static
     * in order to work properly with current Spring behavior.
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
        Resource[] resources = new ClassPathResource[] { new ClassPathResource(PROPERTY_FILENAME) };
        pspc.setLocations(resources);
        pspc.setIgnoreUnresolvablePlaceholders(true);
        return pspc;
    }
}
like image 21
fade858 Avatar answered Nov 02 '22 09:11

fade858