...
First of all we need an object to store the prices, our configurable object:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package pricing; import org.configureme.annotations.Configure; import org.configureme.annotations.ConfigureMe; @ConfigureMe public class Pricing { /** * The local currency. */ @Configure private String currency; /** * The price of the BigPack in local currency. */ @Configure private float price; public void setCurrency(String aCurrency){ currency = aCurrency; } public void setPrice(float aPrice){ price = aPrice; } /** * Returns the price for the customer. */ public String getProductPrice(){ return price+" "+currency; } } |
...
Now that we have a configurable we need a configuration.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
{ price: 3.57, currency: "$", } |
...
Now that we have prepared everything, the first customer can come in. All we need is to add 3 more lines of code:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package pricing; import org.configureme.ConfigurationManager; public class ShowPrice { public static void main(String a[]){ Pricing pricing = new Pricing(); ConfigurationManager.INSTANCE.configure(pricing); System.out.println("Please pay "+pricing.getProductPrice()); } } |
...
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:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
{ price: 3.57, currency: "$", europe: { //many european countries have euro as currency currency: "€", uk:{ price: 2.29, currency: "£", }, de: { price: 3.00, }, at: { price: 3.50, }, }, } |
...
Now that your BigPack business is flourishing you don't need to work anymore and spent time with things that matter. You even gave away the pricing policy to a high ranked manager in your conglomerate. However, for the sake of good old times you want to check the sales and prices from time to time. But doing it with the method from last paragraph is rather uncomfortable, since you have to travel to the target country to check the price in it. Fortunately for you ConfigureMe offers a possibility to specify execution environment explicitly:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class ShowPrice { public static void main(String a[]){ showPriceIn("USA", GlobalEnvironment.INSTANCE); showPriceIn("United Kingdom", new DynamicEnvironment("europe", "uk")); showPriceIn("Germany", new DynamicEnvironment("europe", "de")); showPriceIn("Austria", new DynamicEnvironment("europe", "at")); } private static void showPriceIn(String description, Environment environment){ Pricing pricing = new Pricing(); ConfigurationManager.INSTANCE.configure(pricing, environment); System.out.println("Price in "+description+" is "+pricing.getProductPrice()); } } |
...
Finally you may ask (and actually people asked ), if all fields in the class, as in the above example, are configureable, why they all must be annotated? They don't. In fact you can add a parameter to the ConfigureMe annotation telling it, that all fields are configureable:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
... @ConfigureMe (allfields=true) public class Pricing { private String currency; private float price; ... |
...
This is quite easy. ConfigureMe has Spring support allready included, just use the org.configureme.spring.ConfigureSpringBeans class and add all SpringBeans that should be configure as constructor argument.
For example when using the default environment:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<bean id="pricing" class="Pricing" /> <bean class="org.configureme.spring.ConfigureSpringBeans"> <constructor-arg ref="pricing" /> </bean> |
you can also specify the a environment which should be used when adding a DynamicEnvironment as first constructor argument.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<bean id="pricing" class="Pricing" /> <bean class="org.configureme.spring.ConfigureSpringBeans"> <constructor-arg> <bean class="org.configureme.environments.DynamicEnvironment"> <constructor-arg value="europe" /> <constructor-arg value="de" /> </bean> </constructor-arg> <constructor-arg ref="pricing" /> </bean> |
...