7 Replies Latest reply on Dec 15, 2009 5:40 PM by Rob Shepherd

    Dependency Injection @In problem

    Thiago Prudente Newbie

      Hi everyone!


      I'm facing a problem when I try to inject that dependency in my class.


      I'm getting the NullPointerException


      I wanna know what could be happening to that injection isn't working.


      thanks!

        • 1. Re: Dependency Injection @In problem
          Rob Shepherd Newbie

          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
            Tim Evers Master

            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
              Thiago Prudente Newbie

              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
                Nikolay Elenkov Master

                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
                  Rob Shepherd Newbie

                  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 the Service 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 the Service instead of always using
                  (create=true)

                  where they are used.


                  Hope this helps


                  Rob

                  • 6. Re: Dependency Injection @In problem
                    Thiago Prudente Newbie
                    thanks Rob!

                    I used create=true but it isn't working!

                    • 7. Re: Dependency Injection @In problem
                      Rob Shepherd Newbie

                      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 above
                      execute(...)

                      method.