9 Replies Latest reply on Dec 29, 2009 6:37 PM by gavin.king

    Disposing and InjectionPoint integration?

    meetoblivion

      So let's say I have an injection point defined something like


      @Inject @RequestScoped @MyQualifier(type=A,name="Charlie") SomeBean someBean;


      When processing the injection, it's easy to just call the init() method on SeamBean, because there's stuff I want to do.  However, when an instance of this goes away (at the end of the session), I need to call a cleanUp() method on SeamBean.  The trouble is that there are many injection points for this same bean within the same request lifecycle (at least, if I've read right, it's the same bean going into each injection point).  Is this a case where I want to use @NonBinding on the methods in @MyQualifier?  Then I can define a dispose method




      public void disposeOfSomeBean(@Inject @RequestScoped @MyQualifier SomeBean someBean) {
      someBean.cleanUp();
      }



        • 1. Re: Disposing and InjectionPoint integration?
          meetoblivion

          ack, should be @Disposes not @Inject.

          • 2. Re: Disposing and InjectionPoint integration?
            gavin.king

            I don't completely understand the question. What is the scope of the bean? What is init()? A @PostConstruct method? What is cleanUp()? A @PreDestroy method?


            In general, you can do the same things with @PreDestroy that you can do with a @Disposes method. The only difference is that @Disposes is for @Produces methods.

            • 3. Re: Disposing and InjectionPoint integration?
              meetoblivion

              In this case, I do not control SomeBean (in fact, in my particular scenario its a javax.jcr.Repository, or a javax.jcr.Session, depending on how used).  Instances of it have to be created via producers rather than pure annotations.  I'm attempting to use Session the same way one used to use hibernate's session on a session per request style.  My goal is to have an annotation.


              Long story short, I need an automated way of calling the cleanUp() method without modifying the bean.  Will @Disposes dispose of all instances, or will I need to qualify each individual instance?

              • 4. Re: Disposing and InjectionPoint integration?
                gavin.king

                If you want to write a single disposer method that matches all beans of a certain type, regardless of their qualifier, you should use the qualifier @Any. Just like any other injection point.

                • 5. Re: Disposing and InjectionPoint integration?
                  meetoblivion

                  with Any, since it sounds like the dispose method should be in the same class as the producer, will it also catch instances created by producers in different classes?

                  • 6. Re: Disposing and InjectionPoint integration?
                    gavin.king

                    No, disposer methods apply to producers defined by the same bean.

                    • 7. Re: Disposing and InjectionPoint integration?
                      nickarls

                      This wasn't always the case in some of the drafts(?) Do you recall why it was changed?

                      • 8. Re: Disposing and InjectionPoint integration?
                        meetoblivion

                        So I ran into an interesting issue.


                        Let's say I'm disposing an object.  In some cases, I will need access to the Field, or maybe InjectionPoint.  Can I use InjectionPoint instead of @Disposes @Qualifier SomeBean someBean?

                        • 9. Re: Disposing and InjectionPoint integration?
                          gavin.king

                          John Ament wrote on Dec 29, 2009 02:51:


                          So I ran into an interesting issue.

                          Let's say I'm disposing an object.  In some cases, I will need access to the Field, or maybe InjectionPoint.  Can I use InjectionPoint instead of @Disposes @Qualifier SomeBean someBean?


                          Yes, it is possible to write generic producer/disposer method pairs, for example:



                          class SomeClass {
                          
                              @Produces @Any Foo produceFoo(InjectionPoint ip) { ..... }
                              void disposeFoo(@Disposes @Any Foo foo, InjectionPoint ip) { .... }
                          
                          }



                          Note that you cannot move the InjectionPoint injection up to SomeClass, since it would represent the injection point into which the SomeClass was injected, not the injection point into which the Foo was injected.


                          Please test this out, since it would not surprise me if this is not currently working in Weld. I need to add some language to the spec to make it much clearer that an InjectionPoint injected into a disposer method refers to the producer method bean and not to the declaring bean.