Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h1. 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.

...

Selling hamburgers

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:

Code Block
titlePricing.java
borderStylesolid
 

h2. Selling hamburgers

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.

h3. Creating a configurable

First of all we need an object to store the prices, our configurable object:
{code: title=Pricing.java|borderStyle=solid}
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;
	}
}
{code}
 
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.

h3.

...

 Creating a configuration file.

...



Now that we have a configurable we need a configuration.

...

Code Block
title
 
{code: title=pricing.json
|borderStyle
=solid
}
{
	price: 3.57,
	currency: "$",
}
{code}
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:

Code Block
titleShowPrice.java
borderStylesolid
|Annotations#ConfigureMe]. 

h3. 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:
{code: title=ShowPrice.java|borderStyle=solid}
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("MyPlease Pricepay is "+pricing.getProductPrice());
	}
}
{code}

Once run the output of the program is {{{Please pay 3.57 $}}}