Failure to inject dependency mapped with mapValue
I'm having some issues getting one of my services injected into a model. Right now my ApplicationContext startup looks like this:
override public function startup():void
{
trace(this + ' startup()');
var startup:Signal = new Signal();
signalCommandMap.mapSignal(startup, PrepSignalsCommand, true);
signalCommandMap.mapSignal(startup, PrepServiceCommand, true);
signalCommandMap.mapSignal(startup, PrepModelCommand, true);
signalCommandMap.mapSignal(startup, PrepViewCommand, true);
signalCommandMap.mapSignal(startup, PrepControllerCommand, true);
signalCommandMap.mapSignal(startup, StartupCommand, true);
startup.dispatch();
}
My PrepService command looks like this:
override public function execute():void
{
trace(this + ' running...');
var amfService:AMFService = new AMFService('http://path-to-gateway', 'user');
injector.injectInto(amfService);
injector.mapValue(AMFService, amfService);
}
My PrepModel command looks like this:
override public function execute():void
{
trace(this + ' running...');
injector.mapSingleton(UserModel);
}
Finally in the UserModel I have the following:
public class UserModel extends Actor
{
// NAME
public static const NAME :String = 'UserModel';
// SIGNALS
public var userUpdatedSignal :Signal;
// CLASS MEMBERS
[Inject]
public var amfService :AMFService;
public function UserModel()
{
super();
_init();
}
private function _init():void
{
trace(amfService);
}
No matter what I do amfService is always null in the UserModel.
Any suggestions..?
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Shaun Smith on 08 Jul, 2010 11:06 AM
Hi Rob,
Dependencies injected via setter/property injection are not available until after the instance has been created - it's pretty easy to visualize: the instance must be constructed before any properties can be set by the injector.
Remove the code from your constructor and try this:
[PostConstruct] public function init():void { trace(amfService); }Public methods marked with [PostConstruct] will called after all dependencies have been satisfied.
3 Posted by rob on 08 Jul, 2010 05:53 PM
oh ok.
I was trying to set a listener on the AMFService so instead I'll inject my model into my startup command and call init before anything else. does that seem cool?
btw, thanks for getting back to me :D!
Support Staff 4 Posted by Shaun Smith on 09 Jul, 2010 10:54 AM
No problem! Calling init from a Command sounds OK, but using [PostConstruct] is the standard way to run setup code when using setter injection.
5 Posted by rob on 07 Nov, 2010 06:07 AM
Hey Shaun,
[PostConstruct] is working well for me. Thanks man!