RESTX Settings support
Restx provides a simple yet powerful way to handle your application settings.
Defining a settings interface
First you need to define an interface which will be used to access your application or module settings:
Each method defines a settings key, i.e. a particular setting. You can provide a default value for each setting. The doc parameter allow to add some basic documentation for the setting, which will be displayed in the admin page for the settings.
Using the settings
Then you can simply get an instance of this interface injected in any of your components:
Note that all settings are also made available as Named strings in the factory, so you can get a single setting injected:
Providing the settings
You have several way to define the value of the settings:
Using default value
By giving a default value in the settings key, you can define the value the setting will take by default
Using properties file
You can easily load settings values from a properties file. Restx provide a ConfigLoader component which can be used to load such properties from classpath:
This will try to load settings from to classpath resources:
where [env] is the value of a string named ‘env’ (which can be set using system property).
The properties file are not true properties file, they have the following differences:
- use UTF-8 encoding
- lines beginning with
#are considered to be the documentation of the key following the comment
Here is an example file:
If you want to take advantage of this properties format but don’t want to load it from the classpath, you can also use the parser on its own:
Using named strings
You can provide a named string in the factory to set the value of a setting.
This is useful if you want to do programmatic settings.
Using system properties
System properties set using -Dexample.key1=value will override the value of the setting.
You can access all the settings values in the admin console, on /@/ui/config/.
As you can see the console provides:
- the origin of each value (in the screenshot, key2 was set with system property, key1 in a properties file in classpath, and key3 used the default value from the rxinvoice.MySettings interface)
- the documentation for the key, coming either from the settings interface or from the properties file.