'Merging common Hikari properties with data source specific properties?

I am creating a spring boot app which has multiple data sources (7 in total) and whilst properties like dbUrl, username and password are data source specific, a great many are common. I obviously don't want to duplicate the properties for each data source and am trying to work out how I can create a HikariConfig instance for each datasource using a blended set of common properties.

The properties have the following format

spring.oracle.datasource.driverClassName
spring.oracle.datasource.autoCommit
spring.oracle.datasource.instance[0].dbUrl
spring.oracle.datasource.instance[0].username
spring.oracle.datasource.instance[0].password
spring.oracle.datasource.instance[1].dbUrl
spring.oracle.datasource.instance[1].username
spring.oracle.datasource.instance[1].password
spring.oracle.datasource.instance[2].dbUrl
spring.oracle.datasource.instance[2].username
spring.oracle.datasource.instance[2].password
spring.oracle.datasource.instance[n].dbUrl
spring.oracle.datasource.instance[n].username
spring.oracle.datasource.instance[n].password

I did try using configuration properties with a class that had the format below (lombok annotations ommitted)

public class DataSourceProperties extends HikariConfig {
    public List<Instance> instance;

    public static class Instance {
       public String dbUrl;
       public String username;
       public String password;
    }
}

But although the object is populated correctly I cannot figure out how I then create the n HikariConfig instances using the properties in the pojo. Clone or BeanUtils does not work, as it copies across all of the null fields which are rejected by the HikarConfig setters.

Anyone know a possible solution without duplicating the common properties and resorting to manual creation of the HikariConfig instances?



Solution 1:[1]

In the end, I used Spring's BeanUtils and wrote a function which found the null fields so they could be excluded.

Spring BeanUtils API Doc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 juckky