Date: Tue, 19 Mar 2024 09:34:44 +0100 (CET) Message-ID: <993409849.1216.1710837284708@ixse2nexusrepojiraconflopsrc> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1215_1698594063.1710837284707" ------=_Part_1215_1698594063.1710837284707 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
To get started with ConfigureMe you need three things. First you need so= mething 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 Co= nfigurationManager to configure your configurable= with your configuration somewhere in your code.
Lets get concrete. Imagine you are opening a fast food restaurant. To ma= ke it real fast you offer just one product, the BigPack. Since you are a gr= eat 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.
First of all we need an object to store the prices, our configurable obj= ect:
package= pricing; import org.configureme.annotations.Configure; import org.configureme.annotations.ConfigureMe; @ConfigureMe=20 public class Pricing { =09/** =09 * The local currency. =09 */ =09@Configure private String currency; =09/** =09 * The price of the BigPack in local currency. =09 */ =09@Configure=20 private float price; =09 =09public void setCurrency(String aCurrency){ =09=09currency =3D aCurrency; =09} =09 =09public void setPrice(float aPrice){ =09=09price =3D aPrice; =09} =09 =09/** =09 * Returns the price for the customer. =09 */ =09public String getProductPrice(){ =09=09return price+" "+currency; =09} }
Our configurable is nothing more than a JavaBean which an additional met= hod: getProductPrice_ which return the price we have to show the c= ustomer. Since we are planing to get international pretty soon, we introduc= ed a special field for the currency, to be able to operate in different cou= ntries.
Now that we have a configurable we need a configuration.
{ =09price: 3.57, =09currency: "$", }
Note that the default name resolution is deduced from the class name, bu= t you can change it by parameterizing th= e annotation.
Now that we have prepared everything, the first customer can come in. Al= l we need is to add 3 more lines of code:
package= pricing; import org.configureme.ConfigurationManager; public class ShowPrice { =09public static void main(String a[]){ =09=09Pricing pricing =3D new Pricing(); =09=09ConfigurationManager.INSTANCE.configure(pricing); =09=09System.out.println("Please pay "+pricing.getProductPrice()); =09} }
Once run the output of the program is:
Please pay 3.57 $
=E2=80=94
Now since your business is running well, it's time to become a global pl= ayer and run international branches. To start with you launch 3 internation= al 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:
{ =09price: 3.57, =09currency: "$", =09 =09europe: { =09=09//many european countries have euro as currency =09=09currency: "=E2=82=AC", =09=09uk:{ =09=09=09price: 2.29, =09=09=09currency: "=C2=A3", =09=09}, =09=09 =09=09de: { =09=09=09price: 3.00, =09=09}, =09=09 =09=09at: { =09=09=09price: 3.50, =09=09}, =09=09=20 =09}, }
Done? Almost. Now you simply have to tell the program in which environme=
nt its executed so it can select the proper configuration. In your branch i=
n united kingdom you will have to run the program with following parameter:=
java -Dconfigureme.defaultEnvironment=3Deurope_uk pricing.ShowPri=
ce
The output will be as expected:
Please pay 2.29 =C2=A3
Now that your BigPack business is flourishing you don't need to work any= more 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 sak= e of good old times you want to check the sales and prices from time to tim= e. 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 e= nvironment explicitly:
public = class ShowPrice { =09public static void main(String a[]){ =09=09showPriceIn("USA", GlobalEnvironment.INSTANCE); =09=09showPriceIn("United Kingdom", new DynamicEnvironment("europe", "uk"))= ; =09=09showPriceIn("Germany", new DynamicEnvironment("europe", "de")); =09=09showPriceIn("Austria", new DynamicEnvironment("europe", "at")); =09} =09 =09private static void showPriceIn(String description, Environment environm= ent){ =09=09Pricing pricing =3D new Pricing(); =09=09ConfigurationManager.INSTANCE.configure(pricing, environment); =09=09System.out.println("Price in "+description+" is "+pricing.getProductP= rice()); =09} }
When you are starting the program now it prints the prices in all branch= es, leaving you enough time for your other projects:
Price in USA is 3.57 $
Price in United Kingdom is 2.29 =C2=A3
Price in Germany is 3.0 =E2=82=AC
Price in Austria is 3.5 =E2=82=AC
Finally you may ask (and actually people asked :smile:), if all fields i= n 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 ConfigureM= e annotation telling it, that all fields are configureable:
... @ConfigureMe (allfields=3Dtrue)=20 public class Pricing { =09private String currency; =09private float price; ...
If you run this program now, you will see that the results are exactly t= he same as in previous version.
This is quite easy. ConfigureMe has Spring support allready included, ju=
st use the org.configureme.spring.ConfigureSpringBeans class and add all Sp=
ringBeans that should be configure as constructor argument.
For example when using the default environment:
<bea= n id=3D"pricing" class=3D"Pricing" /> <bean class=3D"org.configureme.spring.ConfigureSpringBeans"> <constructor-arg ref=3D"pricing" /> </bean>
you can also specify the a environment which should be used when adding = a DynamicEnvironment as first constructor argument.
<bea= n id=3D"pricing" class=3D"Pricing" /> <bean class=3D"org.configureme.spring.ConfigureSpringBeans"> <constructor-arg> <bean class=3D"org.configureme.environments.DynamicEnvironment"&= gt; =09 <constructor-arg value=3D"europe" /> =09 <constructor-arg value=3D"de" /> =09</bean> </constructor-arg> <constructor-arg ref=3D"pricing" /> </bean>
Download and try ConfigureMe now: https://configureme.dev.jav= a.net