<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Deferring instance creation/injection to CDI
</h3>
<span style="margin-bottom: 10px;">
reply from <a href="http://community.jboss.org/people/ALRubinger">Andrew Rubinger</a> in <i>EJB 3.0 Development</i> - <a href="http://community.jboss.org/message/556161#556161">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><blockquote class="jive-quote"><p>Marius Bogoevici wrote:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Restarting the discussion. In summary, what Weld/CDI needs is:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>1) EJB defers instance creation to an external component</p><p>    - Weld deployer sets the instantiator components that will:</p><p>             a) invoke the applicable CDI constructor of the EJB class</p><p>             b) apply decorators on the EJB</p><p>             c) *not* apply interceptors on the EJB - for now, this remains a responsibility of the EJB container</p></blockquote><p>I believe I've now released something which accounts for this.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-bean-instantiator">http://github.com/jbossejb3/jboss-ejb3-bean-instantiator</a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The above has been released as 1.0.0-alpha-1 and integrated into ejb3-core (ie. we now use the abstraction for EJB bean instance creation).  This has not yet been integrated into AS yet (as that relies upon a release of core, which is a formal release chain).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The general idea is that you create an implementation of the SPI, and create an MC bean called NAME_BEAN_INSTANCE_INSTANTIATOR from</p><p><a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-bean-instantiator/blob/master/spi/src/main/java/org/jboss/ejb3/instantiator/spi/AttachmentNames.java">http://github.com/jbossejb3/jboss-ejb3-bean-instantiator/blob/master/spi/src/main/java/org/jboss/ejb3/instantiator/spi/AttachmentNames.java</a><span> </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Once you do this, the deployer impl will pick it up and wire things along such that the EJBContainer uses it.  So make one artifact which implements jboss-ejb3-instantiator-spi, and inside it put the impl class and a META-INF/weld-bean-instantiator-jboss-beans.xml which may look like:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><?xml version="1.0" encoding="UTF-8"?></span>
  <span class="jive-xml-comment"><!--
    Declares a EJB3 Bean Instantiator
--></span>
<span class="jive-xml-tag"><deployment xmlns="urn:jboss:bean-deployer:2.0"></span>
  <span class="jive-xml-tag"><bean name="org.jboss.ejb3.bean.BeanInstantiator"
    class="org.jboss.ejb3.instantiator.impl.Ejb31SpecBeanInstantiator" /></span>
<span class="jive-xml-tag"></deployment></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Check out the layout of the jboss-ejb3-instantiator-impl module for our current legacy impl.  Basically you'll be writing the same thing. <span> :) </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Also note that we currently don't support any construction parameters.  ejb3-core doesn't have any available to pass along.  So even though the contract accepts construction params, I'm passing in Object[]{}.  If that needs to be addressed...let me know and we'll figure something.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><blockquote class="jive-quote"><p>Marius Bogoevici wrote:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>2) EJB container provides a hook for preprocessing interceptors after instantiation. The main reason is that EJB interceptors may be CDI-injected </p><p>(note: could we reuse the same mechanism as for EJB instantiation, since it boils down to producing an instance of the class? )</p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I'm pretty sure we could do this too.  We have this bit of code, though I haven't verified if it's currently used or cruft:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> Object createInterceptor(Class<?> interceptorClass) <font color="navy"><b>throws</b></font> InstantiationException, IllegalAccessException
   <font color="navy">{</font>
      Object instance = interceptorClass.newInstance();
      InterceptorInjector interceptorInjector = interceptorInjectors.get(interceptorClass);
      <font color="navy"><b>assert</b></font> interceptorInjector != <font color="navy"><b>null</b></font> : <font color="red">"interceptorInjector not found for "</font> + interceptorClass;
      interceptorInjector.inject(null, instance);
      <font color="navy"><b>return</b></font> instance;
   <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Plenty of room in there to both abstract away the instantiation or give some post-instantiation callback before the instance is returned.</p><p><br/>S,</p><p>ALR</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/556161#556161">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB 3.0 Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2030">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>