How to use a constant as the name of a named injection

Chris's Avatar


25 Jul, 2012 05:15 AM

I can say:-

injector.mapSingleton(MyClass, MyClassConstantName);

but how do I declare the injection with that name as:-


doesn't seem to work.

  1. Support Staff 1 Posted by Ondina D.F. on 25 Jul, 2012 06:39 AM

    Ondina D.F.'s Avatar

    Hi Chris,

    For named injections you have to use the 3rd parameter, „named“:

    mapSingletonOf(whenAskedFor:Class, useSingletonOf:Class, named:String = null)

    The correct mapping:
    injector.mapSingletonOf(IDummyService, DummyService, "myDummy");

    Named injection:

    [Inject (name="myDummy")]

    public var service:IDummyService


    mapSingleton(whenAskedFor:Class, named:String="");

    injector.mapSingleton(DummyService, "myDummy");

    [Inject (name="myDummy")]

    public var service:DummyService


  2. 2 Posted by Chris on 25 Jul, 2012 06:53 AM

    Chris's Avatar

    Sorry, but I should have made it clearer that in my example MyClassConstantName is defined as:-

    static public constant MyClassConstanName:String = "myClassConstantName";
  3. Support Staff 3 Posted by Stray on 25 Jul, 2012 07:06 AM

    Stray's Avatar

    Hi Chris,

    unfortunately you can't. Metadata can't take in a constant from the class. The metadata pre-exists the class, so at the point where the metadata is created, it is too early to read your constant.

    (Till would be able to tell you this with more precision, but I'm guessing the bit you care about is that it doesn't work).

    So you'll need to use the string literal.

    This is one of the reasons why some of us really don't like named injection. Others find it useful - I suspect they are better at being accurate than me!


  4. Ondina D.F. closed this discussion on 25 Aug, 2012 02:42 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac