1. ejb-spec
  2. EJB_SPEC-67

Improve stateful bean injection scenarios


    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 3.2
    • Fix Version/s: 3.2
    • Labels:


      The use cases for injection of Stateful session beans into container-managed component types (Stateless, Singleton, MDBs, Servlets) are extremely limited due to the fact that injection occurs when the component class is constructed. In order to get an invocation-based stateful bean, applications must either inject and use 2.x homes or do a JNDI lookup.

      Proposal –

      Provide a type-safe interface for creating stateful session beans that is decoupled from JNDI/EJBContext lookup. The interface would be similar to 2.x-style homes but without requiring the bean provider to specify an explicit home interface.

      The interface looks something like this:

      package javax.ejb;
      public interface SessionFactory<T> {
        T create();

      This interface is bound into the java:module/app/global namespace for stateful beans using this new interface.


      • java:module/Bean!com.foo.ejb.BizIntf!javax.ejb.SessionFactory

      Example usage:

      // Stateful bean definition
      public class MyStateful implements BizIntf {
        private String something;
        public String getSomething() {
           return something;
      // Client-side injection and EJB ref (for completeness) of factory using annotations:
      @EJB(name="ejb/bizfactory", beanInterface=BizIntf.class, sessionFactory=true)
      public class MySingleton {
        private SessionFactory<BizIntf> factory;
        private SessionContext ctx;
        public myMethodInjection() {
          BizIntf biz = factory.create();
          String s =  biz.getSomething();
        public myMethodLookup() {
          SessionFactory<BizIntf> factory = (SessionFactory<BizIntf>)ctx.lookup("ejb/bizfactory");
          BizIntf biz = factory.create();
          String s = biz.getSomething();

      ejb-ref definition in the DD:


      Primary Motivations:
      1. Injection and type safety - Again, SFSB's can't be usefully injected into most container-managed component types, so you must use lookup, which requires a cast.
      2. Performance - eliminates the need for a lookup on each client invocation. Also decouples SFSB creation performance from JNDI, since lookup performance is limited by/bound to the performance of naming.
      3. A means for enabling use of stateless beans with OSGi service registry - Vendors are starting to experiment with EJBs in OSGi service registry, which has no mechanism for creating multiple interfaces (ServiceFactory doesn't work since it's once-per-bundle.)

      Other considerations/possible additions:
      1. A create method which takes a variable argument list(Object... args). It would not be type-safe, but would provide a path to support @Init.
      2. A RemoteSessionFactory for business RMI that throws RemoteException.
      3. A ManagedBeanFactory that provides a factory interface for @ManagedBean. Would need to find a home for that interface (!javax.annotations). Also, would require coordination with managed beans spec.


        No work has yet been logged on this issue.


          • Assignee:
          • Votes:
            0 Vote for this issue
            0 Start watching this issue


            • Created: