-
1. Re: Dependency Injection @In problem
robshep Dec 14, 2009 3:55 PM (in response to thiagohcprudente)My Guess is that you are trying to use the dependency in the constructor of the dependent service.
you can't use injection in constructors.
See the @Create annotation for calling a method for setting up an object after it has been created.
-
2. Re: Dependency Injection @In problem
kragoth Dec 14, 2009 11:54 PM (in response to thiagohcprudente)What Rob said is quite a common problem however, if that does not fix your problem please post your code because there are other reasons why this can happen.
-
3. Re: Dependency Injection @In problem
thiagohcprudente Dec 15, 2009 11:55 AM (in response to thiagohcprudente)The code next, is a class that is executed when is on the time defined by quartz API, so its method is executed and then I try to use the injected service.
As you can see, I used @AutoCreate, to force the creation of the instances.
@Name("alertaDesempenhoQuartzService") @AutoCreate public class AlertaDesempenhoQuartzService implements StatefulJob { public static String nextFire = ""; public AlertaDesempenhoQuartzService() { } @In private AlertaDesempenhoService alertaDesempenhoService; @In private ParametrosService parametrosService; @In private ResponsavelService responsavelService; public void execute(JobExecutionContext context) { String dataBase = context.getJobDetail().getJobDataMap().getString("dataBase"); Collection<Parametros> parametros = parametrosService.pesquisaAlertaDesempenhoAtivo(); } }
Thanks in advance!
-
4. Re: Dependency Injection @In problem
kapitanpetko Dec 15, 2009 12:55 PM (in response to thiagohcprudente)Read the docs one more time: @AutoCreate doesn't create the instances. Use @In(create=true) instead. You can annotate your services with @AutoCreate for the same result, but since they are probably stateless, @In(create=true) is the better option.
HTH
-
5. Re: Dependency Injection @In problem
robshep Dec 15, 2009 1:01 PM (in response to thiagohcprudente)Thiago,
Just in case you've mis-understood the @AutoCreate annotation, here's some explanaition....
@AutoCreate is used to permit SEAM to make a new instance of the
Service
is one does not exist.
@AutoCreate is used on theService
implementation not the class that uses (depends on) it.so in your case above SEAM will make a new instance of
AlertaDesempenhoQuartzService
with the instance name of
alertaDesempenhoQuartzService
But without seeing your code we can't tell if SEAM should be allowed to
AutoCreate
your injected services.just try changing the injection of the
services
from ....@In
to
@In(create=true)
If this works, and you consider that these servies will Always need
AutoCreating
, then put@AutoCreate
in theService
instead of always using(create=true)
where they are used.Hope this helps
Rob
-
6. Re: Dependency Injection @In problem
thiagohcprudente Dec 15, 2009 1:23 PM (in response to thiagohcprudente)thanks Rob!
I used create=true but it isn't working! -
7. Re: Dependency Injection @In problem
robshep Dec 15, 2009 5:40 PM (in response to thiagohcprudente)OK Thiago,
More information is needed to help you.
Which line is failing? post a stacktrace or the exception output maybe.
Post the basic source code ( no method bodies needed, just declarations and annotations etc ) for this class and an example of a dependency. E.g. One of your Services, probably
ParametrosService
as this is the one used in the aboveexecute(...)
method.