Home > Software design >  How can I map properties from spring application.properties to java object using lombok, when multip
How can I map properties from spring application.properties to java object using lombok, when multip

Time:08-19

In application.properties,we have something like below property

logs.dir = d://log

And using java object, we can get the application property value like below:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties
public class ApplicationBean {
    private Logs logs = new Logs();
    
    @Data
    public class Logs {
    private String dir;
    }
}

but when we have multiple dots(.) and hyphen(-) in property name like

log.import-data.dir = d:\import

i can't able to map it with java object.

Any suggestion?

CodePudding user response:

You should have a prefix to group your config properties. Use CamelCase to convert properties that include a hyphen Create extra classes for nested properties

example:

prefix.logs.import-data.dir = some directory
prefix.logs.site.location = some location
@Data
@Configuration
@ConfigurationProperties(prefix = "prefix") // must have a prefix
class LogProperties {

    private Logs logs = new Logs();

    @Data
    class Logs {
        private Site site = new Site();
        private ImportData importData= new ImportData();
    }

    @Data
    class ImportData {
        private String dir;
    }

    @Data
    class Site {
        String location;
    }
    
}
  • Related