|
To download source of this example click [ here ].
First of all let's create two classes which we will be using in following example.
Class SimpleSingleton implements singleton pattern. |
|
class org.asioc.example.SimpleSingleton {
var instance : SimpleSingleton = null;
public function SimpleSingleton() {
}
public function getInstance() {
if (this.instance == null) {
this.instance = new SimpleSingleton();
}
return this.instance;
}
public function getClassName() : String {
return "org.asioc.example.SimpleSingleton";
}
}
|
|
Class SimpleClass implements simple java beans object. |
|
class org.asioc.example.SimpleClass {
var varible : String = null;
public function getVarible() : String {
return this.varible;
}
public function setVarible(v : String) {
this.varible = v;
}
}
|
|
Now lets configure our environment.
The first way is to create XML file "asioc-config.xml":
|
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE asbeans PUBLIC "-//ASIOC//DTD ASBEANS 0.1//EN"
"http://asioc.sourceforge.net/dtd/asioc-beans.dtd">
<asbeans>
<asbean
id="SimpleClass"
class="org.asioc.example.SimpleClass"
/>
<asbean
id="SimpleSingleton"
class="org.asioc.example.SimpleSingleton"
factory-method="getInstance"
singleton="true"
lazy-init="true"
/>
</asbeans>
|
|
The second way is to write code to configure in fly our IoC Container. |
|
|
Ok. Now we are ready to use IoC Container.
You can get instance of your classes everywhere you want to.
Let's see:
|
|
import com.webservicesware.ioc.IoCLoadConfigObservable;
import com.webservicesware.ioc.IoCContainer;
import com.webservicesware.ioc.IoCUtil;
import com.webservicesware.ioc.IoCConfig;
import logging.LogManager;
/**
* @author Rafal Malinowski
*/
class org.asioc.example.App implements IoCLoadConfigObservable{
public static function main():Void {
//disable logging
LogManager.getInstance().disableLogging();
var app:App = new App();
app.start();
}
public function start() {
trace("Start load XML Configuration "+
" from asioc-config.xml.");
var ioc:IoCContainer = IoCUtil.getIoCContainer();
var iocfg:IoCConfig = IoCUtil.getIoCConfig();
iocfg.addOnLoadConfigListener(this);
iocfg.configureIoCContainer();
}
public function onIoCLoadConfig() : Void {
trace("On configuration load.");
var ioc:IoCContainer = IoCUtil.getIoCContainer();
var simpleObj : org.asioc.example.SimpleClass = null;
simpleObj = ioc.getComponentInstance("SimpleClass");
trace("simpleObj.getVarible() return: "+
simpleObj.getVarible());
simpleObj.setVarible("Sample Varible");
var simpleObj2 : org.asioc.example.SimpleClass = null;
simpleObj2 = ioc.getComponentInstance("SimpleClass");
trace("simpleObj2.getVarible() return: "+
simpleObj2.getVarible());
var single : org.asioc.example.SimpleSingleton = null;
single = ioc.getComponentInstance("SimpleSingleton");
trace("single.getClassName() return "+
single.getClassName());
}
}
|
|
If you run this example you should see something like this: |
|
Start loading XML Configuration from asioc-config.xml.
Start testing IoC Container.
simpleObj.getVarible()
return: null
simpleObj2.getVarible()
return: Sample Varible
sSingletonObj.getClassName()
return: org.asioc.example.SimpleSingleton
|
|