//Class defination
@ConfigurationProperties(prefix="my")
@Component
public class MyProperty {

    String test1;

    Long test2;

    //getter and setter
}
#application.yml
#Just declear the variables with no values 
my:
  test1:
  test2:

In the above case, the variable test1 in String type will be initilized with an empty string (i.e. ""), but the variable test2 will be initialized with a null, which is somehow inconsistant. This could be seen as a feature for avoiding the null pointer exceptions for String initialization, but it can also cause a bug when I thought I would have a null for my variable test1 just like the test2 because of the inconsistent initialization rules.

Also, it will cause another problem: if I set a default value for my variable test1 in the field declaration, and declare the varibale in application.yml with no value, Springboot will use an empty string to replace my declared default value, which will easily cause a problem.

Comment From: pruidong

Spring Boot cannot tell whether your intention is to set the default value to null or "" on purpose, or to accidentally forget to set the value.

I think you should avoid using null or "" as default value in config.

Comment From: LiuLiujie

Thank you for your reply. I understand that spring-boot cannot tell my intention, and I know I am not doing a best practice. But what I thought not right are: 1. Doing one more step to change the null to an empty string for String type seems unnecessary to spring-boot 2. Inconsistent behaviors in dealing with fields with no values in the config file.

For example, in the following case, the variable test 1 will be replaced with an empty string but the variable test2 will not be replaced with the null. This also contradicted the spring-boot document in [default-value] because the document didn't show what should be like in the config file for the name field. But definitely if the user declares the name field in config file with no value, the default value "myName" in MyProperties class will not be set properly.

//Class defination
@ConfigurationProperties(prefix="my")
@Component
public class MyProperty {

    String test1 = "123";

    Long test2 = 123L;

    //getter and setter
}
#application.yml
#Just declare the fields with no values 
my:
  test1:
  test2:

Comment From: snicoll

Unfortunately, there is nothing we can do as Spring Framework treats the presence of a property as an empty String.

Duplicates #24133