title: How to fill @EJB annotated fields programmatically via glassfish container?
body: <p>I have a following problem. I have defined an interface:</p>
  
  <pre><code>@Local
  public interface ProductTypeLister {
     Collection&lt;ListElement&gt; getList();
  }
  </code></pre>
  
  <p>Also, I have defined an implementation for this thing:</p>
  
  <pre><code>@Stateless(name = "ProductTypeLister")
  @Local(ProductTypeLister.class)
  public class ProductTypeListerImpl implements ProductTypeLister {
     @Override
     public Collection&lt;ListElement&gt; getList() {
        // Implementation code
     }
  }
  </code></pre>
  
  <p>And now I want to use it via annotation:</p>
  
  <pre><code>public class ListProductTypeAction extends ActionHandler {
     @EJB
     protected ProductTypeLister lister;
  
     @Override
     public String execute(HttpServletRequest request) throws ServletException {
        request.setAttribute("list", lister.getList());
        return "listPage.jsp";
     }
  }
  </code></pre>
  
  <p>But I get the NullPointerException, since lister is not filled. Now, the problem is, that ActionHandler is not a derivative of HttpServlet and is not executed by the container itself. Instead, we are using FrontController patter - i.e. there is one servlet that handles all the incoming messages and creates required handlers via a simple construction. And therefore the EJB annotated fields are not filled automatically. However, I can fill this specific case in the following manner:</p>
  
  <pre><code>public ListProductTypeAction() throws NamingException {
    InitialContext context = new InitialContext();
    lister = (ProductTypeLister) context.lookup("&lt;PATH&gt;/ProductTypeLister");
  }
  </code></pre>
  
  <p>In that case everything works fine, but this means I have to do for every handler the specified lookup with JNDI help, but all I want is to get it with annotation help. Therefore, the main servlet needs the following method:</p>
  
  <pre><code>private void fillHandler(ActionHandler handler) {
    // Fill @EJB annotated fields
  }
  </code></pre>
  
  <p>But how can I fill it? Of course, I can manually run through every field and check if it's annotated EJB and fill it using JNDI based on the interface unqualified name. But is there a way to do this using the libraries I already have? After all, Glassfish is supposed to fill those fields during the deployment phase. How can I make him do that for a non-servlet class?</p>