简体   繁体   中英

How can I map properties from spring application.properties to java object using lombok, when multiple dot(.) and hyphen(-) is there?

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?

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;
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM