Mantis - Resin
Viewing Issue Advanced Details
952 minor always 02-20-06 08:51 03-27-06 14:53
anonymous  
ferg  
normal  
closed 3.0.16  
fixed  
none    
none 3.0.19  
0000952: @EJB injection and inheritance
The @EJB injection code is not handling annotations specified in a base class.

I have a little code I have been playing with where I have a series of servlets derived from a custom base class. The base class contains a reference to an EJB that all of my servlets need. I tried to configure this by adding an @EJB method in the base class but the injection never happens.

I found two workarounds for now:
- Do the injection from each derived class. Then things work correctly, but I end up with a lot of duplicate code.
- Manually do the lookup in init() of the base class.
Example:

public class BaseServlet extends ...
{
   protected MyEJB ejb = null;

   ...

   @EJB
   public void setMyEJB(MyEJB ejb) ...
}


public class ConcreteServlet extends BaseServlet
{
   ...

   public void service(...)
   {
        this.ejb.method(); // NPE here - injection is not being done
   }
   ...
}


WORKAROUND: Do the injection from the concrete class rather than the base class

public class ConcreteServlet extends BaseServlet
{
   ...
   @EJB
   public void setMyEJB(MyEJB ejb) { ... }
   ...

   public void service(...)
   {
        this.ejb.method(); // Works fine now...
   }

}

Notes
(0000952)
ferg   
03-27-06 14:53   
ejb/0f24