3 Replies Latest reply on Jan 7, 2010 6:34 PM by sagswe

    Understanding CDI

      I was trying to understand what CDI is all about.
      are my following assumptions corect?.
      1)CDI is a runtime
      2)Bean objects are maintained by CDI runtime
      3)we can refer the objects with some given names using EL.
      4)@Requestscoped means the object is created for the duration of that request and as soon as response is sent back to browser client, that object is garbage collected.
      5)@Sessionscoped means that object can be created anytime during the User Session, but destroyed(or GC'ed) by CDI runtime , once the httpsession session object is destroyed.

        • 1. Re: Understanding CDI
          hirowla.ian.rowlands.three.com.au

          I'm no guru on this, but I think I ask mostly intelligent questions on here so I'll have a go at answering yours!


          I think you need to separate the concept of CDI from web applications. It is independent of web applications, although some of the features are designed to assist web application development. CDI is according to the documentation, Java Contexts and Dependency Injection for the Java EE platform. The main point of it is dependency injection. However there are features which assist in web application development. I wouldn't call it a platform (Java EE is a platform) or a runtime, but it is more than just an API. Somebody feel free to come with an adjective to describe it!


          I think the key things are:



          • Where beans can have injected objects (not all objects can be injected, but most can).

          • What objects can be injected, and how to distinguish between similar objects (there are ways to give hints of what is needed).

          • The lifecycle of these objects and how that relates to what is injected (that's where the scopes come into play).



          Have a look at the documentation for Weld, particularly the first few pages. It might give you some help. Have a look at the Java EE 6 API documentation, in particular the javax.enterprise.context and javax.inject packages.

          • 2. Re: Understanding CDI
            nickarls

            1. Yes, a CDI implementations holds state for a running application so it could be called a container. Like an EJB container.

            2. Yes, beans are templates for instances, instances (or proxies of them) are kept in contexts according to their scope.

            3. Yes, if they are annotated @Named

            4. Yes, they are destroyed according to their specified lifecycle

            5. Yes, a servlet listener will trigger the destruction of the context when the session is destroyed.

            • 3. Re: Understanding CDI

              tack niklas o ian,


              I am sure CDI has usage in broad sense in JEE spectrum, what i am trrrying to understand is, its use from web perspective only. if i annototate a JSF backing bean with @Named, it will be managed by CDI.@Requestscoped objects are created on being referred from view page and destroyed once the response is sent back. @Sessionscoped objects are destroyed once httpsession's invallidate method is called or on session timeout.
              consider this
              1)@Named(x)
                @RequestScoped
                Class X{
                 int a;
                 int b;
                //Setters and getters
                 }
                --------
                @Named (y)
                @SessionScoped
                Class Y{
                int c,d;
                @Inejct X x;
                }


              The object x shud be destroyed once the response is sent back. but it is injected inside the object y, which is session scoped. can the values of 'a' and 'b' be referneced inside obejct y during the entire session?.


              2)If i want to check what objects(and also how many) are maintained by CDI, can i put some printf statement somewhere in Weld implemetation and check?. Any specific idea where those print statements go?. I practised some basic examples of CDI using JSF but if i want to make use of it effectively, i want to know lil inner details.


              3) If session times out, how will this trigger @Sessionscoped objects be destroyed. where in source code shud i look if i want to check this.


              tHanks
              saGar