Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean values in Spring application.properties file?

Tags:

Is it possible to have boolean values in Spring configuration file?

I wrote the following field in my bean:

@Value("${pdk.populatedemo}")
private boolean populateDemo;

but if causes the following exception:

Could not autowire field: private boolean com.inthemoon.pdk.data.DatabaseService.populateDemo; nested exception is org.springframework.beans.TypeMismatchException: 
Failed to convert value of type [java.lang.String] to required type [boolean]; nested exception is java.lang.IllegalArgumentException: 
Invalid boolean value [1;]

here I tried

pdk.populatedemo=1;

in application.properties. I also tried =true and some others.

like image 252
Dims Avatar asked Dec 30 '15 12:12

Dims


People also ask

How do you give a boolean value in properties file?

When the Properties of your file are loaded you can use the Boolean -Class to get the Properties: Boolean. getBoolean("your. property");

How pass boolean value in Yaml?

To represent a string in YAML , we use a single quote ( ' ) or a double quote ( " ) and the actual string value within these quotes. We use the standard numeric literals for integers and floating-point numbers. Boolean values can be represented in YAML using the keywords like true and false or Yes and No .

How are boolean values stored in Java?

The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

Can you use == for boolean in Java?

Java boolean operators are denoted by |, ||, &, &&, <, >, <=, >=, ^, != , ==. These logical boolean operators help in specifying the condition that will have the two return values – “true” or “false”.


1 Answers

The correct value for a boolean type would be

pdk.populatedemo=true

1 is not a valid value for a boolean field and you must not use semicolons in your property file for a boolean value (as you clearly can see in the error message).

like image 197
dunni Avatar answered Sep 19 '22 13:09

dunni