4 Replies Latest reply on Jan 10, 2007 5:48 AM by igorla

    Injection and disinjection control

    msystems

      I have two Java Beans, Bean A (scope: session) and Bean B (scope: event). Bean A is injected
      into Bean B. E.g.

      Class A {
      ...
      ...
      ...
      }

      Class B {
      protected Object var;

      @In(value = "ASession", create = true)
      void setVar(Object var) {
      this.var = var;

      do something !
      ...
      ...
      }

      void doSomething1() {
      }

      void doSomething2() {
      }

      void doSomething3() {
      }

      ...
      ...
      }

      Each method invocation on Bean B results in an injection and disinjection.

      In mine particularly case it's totally unnecessary to inject and disinject at each method invocation. I only need
      an injection when an instance of class B is created.

      Because of the current design of Seam my setVar() method is executed between 500 - 5000 (depends on the situation)
      times by the BijectionInterceptor.bijectTargetComponent() and the state of the injected object is the same each time.

      It would be very nice, if I could control the injection and disinjection, e.g.

      @In(oneInjection = true, disinjection = false, value = "ASession", create = true)
      void setVar(Object var) {
      this.var = var;

      do something !
      ...
      ...
      }

      oneInjection = true // only inject one time.
      disinjection = false // don't disinject

      Could Seam support something like that?

      Regards
      Kenneth

        • 1. Re: Injection and disinjection control
          gavin.king

          There is a way, namely that you can use "configuration" instead of bijection. ie.


          <component name="b">
           <property name="var">#{a}</property>
          </component>


          However, it sounds to me like you are trying to preoptimize. A few hundred calls of a setter method is totally insignificant compared to the other costs involved (database access, rendering the view, etc).

          • 2. Re: Injection and disinjection control
            spambob

            Could you please elaborate a bit why injection is happening for every method call instead of only 1 time on the 1st call.

            I'm asking because I thought that configuration with xml or with annotations leads to the same result.
            Further, if i declare a request scoped bean I expect it to be there and exactly the same for the whole life cycle of the request - therefore it would be less preoptimization than avoidance of unnecessary work.

            The only reason i first could have thought of was because of some object caching / pooling issues but as this has to work for annotation configured stuff too it can't be the reason.

            So please elaborate / clarify this a bit.

            Thanks in advance

            • 3. Re: Injection and disinjection control
              gavin.king

               

              Could you please elaborate a bit why injection is happening for every method call instead of only 1 time on the 1st call.


              Because the value of a contextual variable can change b/w calls.

              I'm asking because I thought that configuration with xml or with annotations leads to the same result.


              It definitely does not, and the docs make this very clear.


              Stop pre-optimizing! The FIRST rule of performance is don't make assumptions about performance before you actually have a real application which has a performance problem. This is a basic rule that all good programmers follow. Seam's bijection is NOT going to make your fast application slow!

              • 4. Re: Injection and disinjection control
                igorla

                Is it possible to config injection in such way that injection will be done only on instance create?

                Consider the following code:

                class A{

                @In
                B b;

                C c;
                public getC(){..}

                }

                class C{

                A a;

                public getName(){
                a.getB().getName();
                }

                }

                in .xhtml file present the following reference:
                #{a.c.name}


                What happens: C.getName is called - A instance doesn't have anymore reference to B due to disinjection after call A.getC() -> NPE is thrown.

                The code above is simplified and making C as component is not desirable.
                Probably this code is too much coupled, but it doesn't break any Java rules.