13

Hi I want to autowire boolean value from properties file have referred following link with maps url Spring properties (property-placeholder) autowiring

but I want to auto wire a boolean property, also have referred question Spring Autowire primitive boolean Spring Autowire primitive boolean but that was for bean value and in my case I want to do the same using property value which is dot separated.

${does.it.allow} // which fails and gives String cannot be cast to boolean #{does.it.allow} // this gives no bean/property defined with name does but I have the correct property file and it proves that container is able to load it because of first error.

Community
  • 1
  • 1
Aniruddha
  • 161
  • 1
  • 1
  • 7

2 Answers2

16

It doesn't work for me with primitive boolean. But it does with Boolean type.

This is my spring configuration declaration of the properties file:

<context:property-placeholder location="classpath:path/to/file/configuracion.properties" />

This is what i have in my properties file:

my.property=false

And this is my successful service class:

...
@Service
public class MyServiceImpl implements MyService{
...
    @Value("${my.property}")
    private Boolean nameOfProperty;
...
Carlos
  • 1,257
  • 2
  • 25
  • 38
4

At least from Spring 5 (I didn't test previous versions), you can autowire boolean primitive.

In application properties:

my.property=true

In your class:

@Value("${my.property}")
private boolean myProperty;
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • You can also autowire wrapped Boolean datatype with Spring 5. Tried in my workspace, works like a charm :) – realPK Jul 12 '19 at 21:44