[jboss-user] [EJB 3.0] - Re: @EJB injection in JBoss 5 application clients?

jaikiran do-not-reply at jboss.com
Fri Oct 24 04:18:00 EDT 2008


"bcn" wrote : 
  | 1. Can you really use the EJB annotation only in the main class (and only for static fields)? 
  | 

That's what the spec says.

"bcn" wrote : 
  | In real-world application that would make it almost useless.
  | 
  | 

Why do you think so?

>From what i understand, they do have a valid reason for making this restriction. Consider you have a Main class into which you have to inject the bean. Remember that when you use 'java' to run this class, it invokes the static main method. So if you want to access the injected EJB, that member too should be static. In some of the examples, users usually create an object of the same class in the static main method and then use the instance variables:

  | public class MyClass {
  | 
  |  @EJB
  |  private MyEJB bean;
  | 
  |  public static void main(String args[])
  |  {
  |     MyClass obj = new MyClass();
  |     obj.callMyMethod();
  |  }
  | 
  |  public void callMyMethod() 
  |  {
  |     bean.xxxMethod();
  |  } 
  | }

Consider this scenario shown above. A new object of the class was created and then used to access the bean. The application client container has no way to inject the @EJB for this newly created object (since it has no control on it).

That's the reason (in my opinion) why the field has to be static.


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4184382#4184382

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4184382



More information about the jboss-user mailing list