0
votes

I am building a software system using Java 11 with SpringBoot 2.1. I am thinking about configuration options yet all ways of implementing configuration in Spring I found so far go in other directions. So here is what I want/need:

  1. First, I will have some hardcoded configuration values. They shouldn't be adaptable via a configuration file that is loaded during runtime.

    • Example: Application name.
  2. Second, I want an (internal) properties file for configuration values. These would (mostly) only be edited by developers and would hence serve as standard values when starting the application.

    • Example: application version.
  3. Finally, there will be some configuration values that should be editable by the user during runtime using some UI.

    • Example: Application port

Now, I would like to have a central configuration file, think Singleton pattern, that manages configuration values from all three categories listed above. The idea is that I can access all that from everywhere in the application easily.

Ideally, I'd have a singleton class with a central function taking a config parameter and returning the respective value.

class MyConfig {
    private static singleton = null;
    private MyConfig() {}

    // needed: some name-value storage management for params
    // e.g.: some hardcoded values plus one or more linked property files.

    public static String getProperty(String paramName)
        // fetch parameter and return it
    }

    public static String getProperty(String paramName, String returnType)
        // fetch parameter and return it cast to the specified returnType
    }

    public static String setProperty(String paramName, String value)
        // persist property value to file
    }
}

When starting the application, the configuration should basically do

  1. Load hardcoded values into config object (if not specified in config class itself)
  2. Load values from property file.
    • Values loaded must be checked for validity (e.g. is app_port an integer [1, 65535]).
    • Values from property file must be pre-registered, so a user with write access to the property file cannot "add" a made up new config parameter by adding it.
    • Values from property file must not overwrite hardcoded values.

When the user edits the configuration during runtime, the respective values need to be written back to the properties file (or where-ever they are stored)

Unfortunately, I didn't find anything like this out there and I don't know how to get Java Properties and/or Spring Properties/Configurations to implement something like this.

Anyone, who can point me in the right direction or provide a minimal working example?

1

1 Answers

0
votes

You can load your properties from properties file in MyConfig class constructor into Immutable Map. Make this Immutable Map your class level attribute so that you can access all the properties using this attribute.

class MyConfig {
    public static Map<String, String> immutableMap = null;
    private MyConfig() {
          Map<String,String> modifieableMap = new HashMap<>();
          //code to load properties into modifieableMap
          immutableMap = ImmutableMap.copyOf(mutableMap);
    }

    // needed: some name-value storage management for params
    // e.g.: some hardcoded values plus one or more linked property files.

    public static String getProperty(String paramName)
        // fetch parameter and return it
    }

    public static String getProperty(String paramName, String returnType)
        // fetch parameter and return it cast to the specified returnType
    }

    public static String setProperty(String paramName, String value)
        // persist property value to file
    }
}

If user tries to add or remove any property from Immutable Map, the compiler will throw UnsupportedOperationException exception