-
1. Re: Understanding CDI
hirowla.ian.rowlands.three.com.au Jan 7, 2010 6:31 AM (in response to sagswe)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 Jan 7, 2010 8:53 AM (in response to sagswe)- Yes, a CDI implementations holds state for a running application so it could be called a container. Like an EJB container.
- Yes, beans are
templates
for instances, instances (or proxies of them) are kept in contexts according to their scope. - Yes, if they are annotated @Named
- Yes, they are destroyed according to their specified lifecycle
- Yes, a servlet listener will trigger the destruction of the context when the session is destroyed.
-
3. Re: Understanding CDI
sagswe Jan 7, 2010 6:34 PM (in response to sagswe)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