5 Replies Latest reply on Aug 10, 2009 5:46 PM by Arbi Sookazian

    Dynamic injection in Web Beans?

    Arbi Sookazian Master

      I'm assuming that WB provides a dynamic injection facility similar to Seam.

      So I'd like to know how our apps in terms of DI would work if they were not based on dynamic injection.


      The two major flavors of Dependency Injection are Setter Injection (injection via JavaBean setters); and Constructor Injection (injection via constructor arguments). Spring provides sophisticated support for both, and even allows you to mix the two when configuring theone object.


      So how do Spring/Hibernate apps deal with this limitation?  Static injection means stale data, no?  So is that the difference b/n a stateless (Spring) and stateful (Seam) framework is that one needs static injection and the other needs dynamic injection?

      This was covered a bit in the Seam ref doc but I'm wondering whether Spring 3 will graduate to dynamic injection as well...

      And how DI compares in WB with DI in Spring 3, generally speaking...

        • 1. Re: Dynamic injection in Web Beans?
          Nicklas Karlsson Master

          Hmm, I'm not familiar with Spring 3 injection but since Web Beans injects proxies for normal scoped beans, you are getting the up-to-date instance at invocation time.

          • 2. Re: Dynamic injection in Web Beans?
            Nicklas Karlsson Master

            And you could you producer fields/methods for dynamic resolution.

            For the really-really dynamic version you might be able to add an annotation @Dynamic at the injection point and modify the proxy method handler to re-lookup before invoking ;-)

            • 3. Re: Dynamic injection in Web Beans?
              Pete Muir Master

              I don't get this? The proxy always looks up the most recent version.

              Arbi, what is dynamic injection?

              • 4. Re: Dynamic injection in Web Beans?
                Nicklas Karlsson Master

                I would guess the following scenario:

                • you have and interface Foo

                • and a class Bean1 that implements Foo

                • and a class BeanUsage that injects a field on interface Foo (instance of Bean1)

                • and you runtime add a class Bean2 that implements Foo (higher precedence than Bean1)

                • BeanUsage field would now be an instance of Bean2

                as currently the injection point get wired at creation time to a certain bean (and the proxy makes sure we're hooked up the current instance of that bean) but there is no switching of the bean in question.

                but producer fields/methods can be used to work around this, right?

                • 5. Re: Dynamic injection in Web Beans?
                  Arbi Sookazian Master

                  Pete Muir wrote on Aug 09, 2009 23:51:

                  I don't get this? The proxy always looks up the most recent version.

                  Arbi, what is dynamic injection?

                  From SiA:

                  If you’re familiar with Spring, you might be tempted to use static injection
                  as the primary means of wiring your components together. I don’t
                  recommend that you standardize on this approach. For one, it requires a
                  ridiculous amount of XML. In general, XML should be reserved for infrastructure
                  configuration. Although not applicable in this example, you
                  also must ensure that you aren’t injecting a component from a shorterterm
                  scope into a component in a longer-term scope, as it results in
                  scope impedance. It’s much cleaner, safer, and Seam-esque to wire components
                  together using dynamic injection, declared using the @In annotation

                  So dynamic injection is injection that occurs more than once.  It occurs every time a business method is called on that component.  Static injection occurs only once in the lifetime of the compoment, either via setter method or constructor.

                  Reading my initial post, it has more to do with Spring that Seam/WB but I guess I'll re-word it so it's applicable to this forum:

                  If WB/Seam3 uses dynamic injection (most likely it does - at least Seam 2.x does), what would happen if it was designed with static injection instead?  What limitation(s) would that bring to our apps?  I'm just wondering how Spring apps deal with this stale injection problem on a continual basis and if it is a problem in a stateful or stateless architecture or both.

                  I mean, if you can deal with static injection, that means less interceptions and injections than dynamic DI and less overhead per life of a component and overall better performance, no?

                  Here's my point:

                  What if we wanted static DI sometimes and dyamic DI sometimes in the same component or different components?  Mix and match as the use case requires to improve performance.  Or is the performance hit negligible with dynamic DI?  Not all use cases in a Seam app are modeled using stateful components, are they?  What about a report, for example, or a web service consumption/client for a credit card validator service, etc?