Home > Mobile >  In application.yml, boolean field is-enabled always getting read as default value false
In application.yml, boolean field is-enabled always getting read as default value false

Time:09-06

I am working on the toggle module where I have to define toggle status in application.yml file but the surprising part is boolean value defined in yaml file when fetched in spring-boot code is always showing the default value as false however value in yaml file is true.

But we I rename is-enable to enable things work fine, is there any naming conversion restrictions in yaml files?


application.yml file values

name: test-YAML
environment: testing
is-enabled: true

Config File goes as below:-

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
@Data
public class YAMLConfig {
    private String name;
    private String environment;
    private boolean isEnabled;
    private List<String> servers = new ArrayList<String>();
    private List<String> external = new ArrayList<String>();
    private Map<String, String> map = new HashMap<String, String>();
    private Component component = new Component();
}

I am running my main application like this

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    @Autowired
    private YAMLConfig myConfig;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.run();
    }

    public void run(String... args) throws Exception {
        System.out.println("name:"   myConfig.getName());
        System.out.println("enabled:"   myConfig.isEnabled());
    }

Let me know if more details required. I using sample project from project-from-git

CodePudding user response:

This is beacause your properties will be bind using setters if you are using default constructor.

See docs

So, when you have class like this:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
@Data
public class YAMLConfig {

    private String name;
    private String environment;
    private boolean isEnabled;
...
}

As you know, Lombok will generate getters and setters for you, so it will look like this:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
@Data
public class YAMLConfig {

    private String name;
    private String environment;
    private boolean isEnabled;
    private List<String> servers = new ArrayList<String>();
    private List<String> external = new ArrayList<String>();
    private Map<String, String> map = new HashMap<String, String>();

    ...



    public boolean isEnabled() {
        return isEnabled;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }
}

If you have property is-enabled in your yml file, since this is boolean value, spring will look for setter setIsEnabled in your class, and since that setter doesn't exist it won't bind value for you, soisEnabled property will be set to false by default.

if you add manually setter like this:

 public void setIsEnabled(boolean enabled) {
        isEnabled = enabled;
    }

Your property should be bind.

BTW, you shoud avoid using @ConfigurationProperties withot specifying prefix.

  • Related