title: Dynamic and programmatic creation of EJB3 Session Beans
body: <p>I have a use case where I want to create dynamically (=programmatically) a Stateless Session Bean (EJB3) instead of declaratively creating a Stateless bean through annotation (@Stateless).</p>
  
  <p>Is there a way of telling the EJB container / CDI that my programme wants to:</p>
  
  <ul>
  <li><p>create a java instance;</p></li>
  <li><p>dynamically register the instance as a Stateless EJB (including any meta information such as transactional context (which method should run in which transaction context and under which TxType (Required, Supports, ...)) and</p></li>
  <li><p>registering it also in the naming context (JNDI / EJB name)?</p></li>
  </ul>
  
  <p>So, in a nutshell:</p>
  
  <p>I have custom code acting as a "Stateless SB" producer/provider and it should be able to let the container know that it wants to register a new instance (class) as a Stateless SB with all expected EJB's behaviours (Tx interceptors etc etc).</p>
  
  <p>So what I am really saying:</p>
  
  <blockquote>
    <p>My code knows how to implement a specific logic(by code) which needs to
    be dynamically encapsulated in an instance which belongs to SOME class
    that is NOT declaratively annotated with the @Stateless annotation and
    will be dynamically (runtime) decorated in such a way to behave like a Stateless session
    bean.</p>
  </blockquote>
  
  <p>Another way to approach this, IMO:</p>
  
  <p>To define all EJB specific meta information in the interface which would be implemented by a bean, but EJB3 spec does NOT allow you to declare @Stateless on interface level. @Stateless should be declared on implementation level (bean).</p>
  
  <p><em>Mind you: it is precisely this STATELESS ANNOTATED bean implementation which does NOT exist in my case. So there is no way for the container to scan the code, to find and to register a class as a stateless bean.</em></p>
  
  <p><em>Any ideas?</em></p>