tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/272-injector-is-null-in-custom-classRobotlegs: Discussion 2011-05-21T07:54:13Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/58017962011-03-08T07:45:51Z2011-03-08T07:45:51Zinjector is null in Custom class.<div><p>Hi,</p>
<p>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.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/58017962011-03-08T08:54:59Z2011-03-08T08:54:59Zinjector is null in Custom class.<div><p>You don't need to extend Actor or any other class to use
injections.<br>
You <em>do</em>, however, need to create your class using the
injector like<br>
so: <code>var myInstance: MyClass =
injector.instantiate(MyClass);</code> or,<br>
after manually creating it, inject into it like so:<br>
<code>injector.injectInto(myInstance);</code>.</p>
<p>If you're already doing that, make sure that you're not using
the<br>
injected value(s) too early. When using setter injections (as you
do),<br>
you can't use the injected values in your constructor. Instead,
you<br>
have to wait for the injector to finish the injection. To notify
you<br>
about that, the injector invokes all methods marked with the<br>
<code>[PostConstruct]</code> metadata after injection is
finished.</p>
<p>A boiled-down version of your class could thus look like the
following:</p>
<pre>
<code>public class MyClass
{
[Injector] public var injector : IInjector;
public function MyClass()
{
//don't use the injector here
}
[PostConstruct]
public function initialize()
{
//use the injector here
}
}</code>
</pre>
<p>Another option would be to use constructor injection. To use
that,<br>
simply add the injector as a parameter to you constructor without
any<br>
metadata and make sure to create your instance using the
injector.<br>
This way, you can use the injector in your constructor, of
course.</p>
<p>Oh, and also: You don't need to add a mapping for IInjector,
the<br>
<code>Context</code> class already does that for you.</p></div>Till Schneidereittag:robotlegs.tenderapp.com,2009-10-18:Comment/58017962011-03-08T09:10:05Z2011-03-08T09:10:05Zinjector is null in Custom class.<div><p>@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.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/58017962011-03-08T09:14:24Z2011-03-08T09:14:24Zinjector is null in Custom class.<div><p>Thanks, tschneidereit. What I missed, is the understanding of
<code>injector.instantiate()</code> and
<code>injector.injectInto()</code>. It seems more clear now, and it
definitely works.</p>
<p>Thanks again.</p>
<p>Ilya.</p></div>Flex Incubatortag:robotlegs.tenderapp.com,2009-10-18:Comment/58017962011-03-08T09:38:24Z2011-03-08T09:38:24Zinjector is null in Custom class.<div><p>Glad I could help!</p>
<p>@krasimir: The <code>[Inject]</code> metadata is used by the
injector, which is only ever invoked on an instance if that
instance is either created by<br>
the injector (because it itself is a dependency for some other
class<br>
or because it is created using <code>injector.instantiate</code>)
or if you use<br>
<code>injector.injectInto</code> to explicitly apply injections to
the instance.</p></div>Till Schneidereit