• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Injection at Class Level

 
Ram Venkat
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

What is class level injection (I Understood Field / Setter method Injection). but not getting clear view on class level injection.

What is class level injection?
What is the use of class level injection?


Thanks in Advance
[ April 02, 2008: Message edited by: Ram Venkat ]
 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class level injection has more to do with binding a certain object or resource to the local jndi that is easy to reference. This is useful when you need a resource/object in a helper class that is`nt managed.

i.e.
@EJB(name="audit", beanInterface=AuditService.class)
public class .... {
AuditService audit;

@PostConstruct
public void init(){
audit = (AuditService)context.lookup("audit");
}

}
[ April 03, 2008: Message edited by: Derick Potgieter ]
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get it if my understanding is right. you will get object in audit instance variable at runtime using DI then why is the need to use lookup in init method again to get the object from system resource.

Thanks
Nitin


EJB(name="audit", beanInterface=AuditService.class)
public class .... {
AuditService audit;

@PostConstruct
public void init(){
audit = (AuditService)context.lookup("audit");
}

}
 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To tell you the truth, the only benefit that i can see is to use that reference in a helper class called from the class where the class level DI was used.

So lets say init looked like this:

HelperClass hc;

public void init(){
hc = new HelperClass();
}

and helper class

public class HelperClass{
AuditService audit;

public HelperClass(){
....
audit = (AuditSevice) initialContext.lookup("audit");
}
}

This allows you to bind a resource injected at runtime to local jndi.

Remember that if you use DI on field level, you can still access it via jndi, it is just the whole class path + "." classname + field name. Using class level di just makes it easier to access that resource.

This is a quote from bea.com

The class-level annotations declare an entry in the application component's environment but do not cause the resource to be injected. Instead, the application component is expected to use JNDI or component context lookup method to lookup the entry. When the annotation is applied to the class, the JNDI name and the environment entry type must be specified explicitly.


Maybe someone else can explain it better....sorry!
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have seen in some code where ejb referenece is directly injected by container in class level variable so there was no need for lookup. What is confusing me if we can get the reference using annotation then what is the use of doing lookup? Can anybody explain this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic