injector is null in Custom class.

Flex Incubator's Avatar

Flex Incubator

08 Mar, 2011 12:18 AM

I am trying to inject into custom class which does not extend some class from Robotlegs. Basically, I am trying to make the Factory which is described by Stray here

I have injector.mapValue(IInjector, injector); in my context. I injected injector into custom class [Inject] public var injector:IInjector. But when I am trying to access it, it returns null. I believe that I missed something, but I really do not know what exactly.


  1. 1 Posted by krasimir on 08 Mar, 2011 07:45 AM

    krasimir's Avatar


    you should extend Actor at least if I'm not wrong. RL uses metadata parsing for the injection. So without extending your metadata tags are useless.

  2. Support Staff 2 Posted by Till Schneidere... on 08 Mar, 2011 08:54 AM

    Till Schneidereit's Avatar

    You don't need to extend Actor or any other class to use injections.
    You *do*, however, need to create your class using the injector like
    so: `var myInstance: MyClass = injector.instantiate(MyClass);` or,
    after manually creating it, inject into it like so:

    If you're already doing that, make sure that you're not using the
    injected value(s) too early. When using setter injections (as you do),
    you can't use the injected values in your constructor. Instead, you
    have to wait for the injector to finish the injection. To notify you
    about that, the injector invokes all methods marked with the
    `[PostConstruct]` metadata after injection is finished.

    A boiled-down version of your class could thus look like the following:

        public class MyClass
            [Injector] public var injector : IInjector;

            public function MyClass()
                //don't use the injector here

            public function initialize()
                //use the injector here

    Another option would be to use constructor injection. To use that,
    simply add the injector as a parameter to you constructor without any
    metadata and make sure to create your instance using the injector.
    This way, you can use the injector in your constructor, of course.

    Oh, and also: You don't need to add a mapping for IInjector, the
    `Context` class already does that for you.

  3. 3 Posted by krasimir on 08 Mar, 2011 09:10 AM

    krasimir's Avatar

    @tschneidereit: thanks for the respond. I was wrong about the the injection. One question: is that true that if we don't call injector.instantiate the metadata tags are not used.

  4. 4 Posted by Flex Incubator on 08 Mar, 2011 09:14 AM

    Flex Incubator's Avatar

    Thanks, tschneidereit. What I missed, is the understanding of injector.instantiate() and injector.injectInto(). It seems more clear now, and it definitely works.

    Thanks again.


  5. Support Staff 5 Posted by Till Schneidere... on 08 Mar, 2011 09:38 AM

    Till Schneidereit's Avatar

    Glad I could help!

    @krasimir: The `[Inject]` metadata is used by the injector, which is
    only ever invoked on an instance if that instance is either created by
    the injector (because it itself is a dependency for some other class
    or because it is created using `injector.instantiate`) or if you use
    `injector.injectInto` to explicitly apply injections to the instance.

  6. Flex Incubator closed this discussion on 08 Mar, 2011 10:49 AM.

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