Getting started with ConfigureMe
To get started with ConfigureMe you need three things. First you need something to configure which we call configurable. Than you need something to configure your configurable with, that'll be the configuration. And finally you need to ask the ConfigurationManager to configure your configurable with your configuration somewhere in your code.
Lets get concrete. Imagine you are opening a fast food restaurant. To make it real fast you offer just one product. Since you are a great developer you are writing the software for your cash box yourself, and you want the price to come from a configuration file to be able to react to the financial crises fast.
Creating a configurable
First of all we need an object to store the prices, our configurable object:
Our configurable is nothing more than a JavaBean which an additional method: getProductPrice_ which return the price we have to show the customer. Since we are planing to get international pretty soon, we introduced a special field for the currency, to be able to operate in different countries.
Creating a configuration file.
Now that we have a configurable we need a configuration.
Note that the default name resolution is deduced from the class name, but you can change it by parameterizing the annotation.
Showing the price
Now that we have prepared everything, the first customer can come in. All we need is to add 3 more lines of code:
Once run the output of the program is
Please pay 3.57 $
Now since your business is running well, it's time to become a global player and run international branches. To start with you launch 3 international branches, one in germany, one in austria and one in united kingdom. The only thing you need to change is the config file, you just have to specify the different environments:
Done? Almost. Now you simply have to tell the program in which environment its executed so it can select the proper configuration. In your branch in united kingdom you will have to run the program with following parameter:
java -Dconfigureme.defaultEnvironment=europe_uk pricing.ShowPrice}
The output will be as expected:
Please pay 2.29 £.