Dynamic Injection on runtime using BeanFactory

Met with requirement to inject object dynamically at runtime based on Push Message received.

Had a bit hard time finding solution, unusual with Swiz Framework.
Thought of sharing my experience here..
Step 1: In swiz Context assign the bean factory to your Bean Class

messenger.beanFactory = this.beanFactory;

Step 2:
In your bean class, Implement Interface IBeanFactoryAware
public class NativeMessenger implements IBeanFactoryAware

Step 3:
To Satisfy the implementation add the below code:
private var _beanFactory:IBeanFactory;
public function set beanFactory( beanFactory:IBeanFactory ):void{
_beanFactory = beanFactory;
}
Step 4:
Got access to Beanfactory
public var dynamicDAO:AbstractDAO;
protected function consumeHandler(event:MessageEvent =null) : void
{
var daoName:String = event.message.headers[“dynamicdao”];
dynamicDAO = _beanFactory.getBeanByName(daoName).source as AbstractDAO;
}

Now the dynamicDAO is assigned with the bean dynamically based on the consumer Message.

  1. August 3rd, 2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: