Injecting configuration
Tubing provides an annotation which you can use to inject configuration properties in any Tubing bean.
Basic setup
If you only have the default configuration file, called config.yml
there is no special configuration that you need to do. You can use the @ConfigProperty annotation to inject any property.
For example given following configuration:
Option 1: separate configuration bean
And inject the Configuration bean where you need it:
Option 2: Inject property directly where needed
You don't need to create a special Configuration bean, you can also just inject the configuration property directly into the bean where you need it.
Multiple configuration files
Often with larger plugins you might have multiple configuration files to keep things tidy.
When we do have other configuration files we need to tell plugin the we do have multiple configuration files. This can be done by creating a TubingConfigurationProvider bean.
In the above example I load in 2 configuration files. One is the default config.yml
file, the other is a separate configuration file for all database related properties, called databaseConfig.yml
. The second parameter of the ConfigurationFile constructor is the key of the config file. The key is important because this key will be later on used when we want to inject the property.
This is the only thing that you need to do to setup multiple configuration files. When injecting the config property we can use the earlier defined key as prefix.
databaseConfig.yml
Configuration transformer
We often have a bit more complex configuration types. The @ConfigProperty annotation can automatically map simple values like Strings, primitive types and enums, and simple lists. If we need something more complex we can define a transformer. A transformer is a class which will take the value from the FileConfiguration class and transform it into something else. Tubing will inject the result of the transformer into the bean.
Example:
Config.yml
Transformer
The transformer must implement the IConfigTransformer
interface.
Basically this is just a mapper. The below example will simple take the value from the config file and split the String value by commas, and return the list.
Now we can Inject this list into our bean like this:
Tubing will use the transformer and inject the result value
Configuration placeholders
In Tubing it's possible to reference other properties in your property value by using the {{}} notation.
For example:
Last updated