| |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <HTML> |
| <HEAD> |
| <!-- $PAGETITLE --> |
| <TITLE>OpenEJB - Ejb Injection Example</TITLE> |
| <LINK href="http://openejb.apache.org/all.css" rel="stylesheet" type="text/css"> |
| <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" href="openejb.apache.org/ie.css"><![endif]--> |
| |
| <LINK rel="SHORTCUT ICON" href="http://openejb.apache.org/images/favicon.ico"> |
| <SCRIPT language="JavaScript" src="http://cwiki.apache.org/confluence/pages/viewpage.action?spaceKey=OPENEJB&title=functions.js" type="text/javascript"></SCRIPT> |
| <META http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| </HEAD> |
| <BODY> |
| |
| <!-- Delay the loading of the external javascript file needed for labels (as it takes too long to load and visibly holds loading of the page body) --> |
| <!-- To do this without javascript errors over undefined functions, we need to declare stubs here (that are overrided later by the proper implementations) --> |
| <SCRIPT language="JavaScript" type="text/javascript"> |
| function doAddLabel(hideTextfieldAfterAddParam) |
| { |
| // stub |
| } |
| |
| function onAddLabel() |
| { |
| // stub |
| } |
| |
| function showLabelsInput() |
| { |
| // stub |
| } |
| </SCRIPT> |
| |
| <A name="top"></A> |
| <TABLE class="frameTable" cellpadding="0" cellspacing="0" border="0"> |
| <TR class="Row1"> |
| <TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col2"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col3"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| </TR> |
| <TR class="Row2"> |
| <TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3" id="breadcrumbs"> |
| <!-- $TOP_NAV_BAR --> |
| |
| |
| |
| |
| |
| <A href="../OPENEJB/index.html" title="Index">Home</A> | <A href="../OPENEJB/download.html" title="Download">Download</A> | <A href="../OPENEJB/mailing-lists.html" title="Mailing Lists">Lists</A> | <SPAN class="nobr"><A href="http://issues.apache.org/jira/browse/OPENEJB" title="Visit page outside Confluence" rel="nofollow">Issues<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN> |
| </TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> </TD> |
| </TR> |
| <TR class="Row3"> |
| <TD class="Col1"><IMG alt="" class="Row3Img" id="thinLine" src="http://openejb.apache.org/images/line_sm.gif"></TD> |
| <TD class="Col2"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col3"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| </TR> |
| <TR class="Row4"> |
| <TD class="Col1"> |
| <SPAN id="Navigation"> |
| |
| |
| |
| |
| |
| |
| <H3><A name="Navigation-EJB3.0"></A>EJB 3.0</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="ejb-30-specification.html" title="EJB 3.0 Specification">Specification</A></LI> |
| <LI><A href="callbacks.html" title="Callbacks">Callbacks</A></LI> |
| <LI><A href="security-annotations.html" title="Security Annotations">Security</A></LI> |
| <LI><A href="transaction-annotations.html" title="Transaction Annotations">Transactions</A></LI> |
| <LI><A href="ejb-refs.html" title="EJB Refs">EJB Refs</A></LI> |
| <LI><A href="annotations-xml-and-defaults.html" title="Annotations, XML and Defaults">Defaults</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Deployment"></A>Deployment</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="application-discovery-via-the-classpath.html" title="Application discovery via the classpath">Embedded</A></LI> |
| <LI><A href="deployments.html" title="Deployments">Configured</A></LI> |
| <LI><A href="deploy-tool.html" title="Deploy Tool">Command line</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Configuration"></A>Configuration</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="configuration-properties.html" title="Configuration Properties">Properties pt1</A></LI> |
| <LI><A href="system-properties.html" title="System Properties">Properties pt2</A></LI> |
| <LI><A href="understanding-the-directory-layout.html" title="Understanding the Directory Layout">Dir Layout</A></LI> |
| <LI><A href="databases.html" title="Databases">Databases</A></LI> |
| <LI><A href="jndi-names.html" title="JNDI Names">JNDI Names</A></LI> |
| <LI><A href="security.html" title="Security">Security</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Commands"></A>Commands</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="startup.html" title="Startup">Startup</A></LI> |
| <LI><A href="deploy-tool.html" title="Deploy Tool">Deploy</A></LI> |
| </UL> |
| |
| <H3> |
| <A name="Navigation-Feeds"></A> |
| Feeds |
| </H3> |
| |
| <UL class="feeds"> |
| <LI> |
| <A href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false"> |
| <IMG src="http://openejb.apache.org/images/rss.gif"></A> |
| <A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">Site</A> |
| </LI> |
| |
| <LI><A href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB"> |
| <IMG src="http://openejb.apache.org/images/rss.gif"></A> |
| <A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">News</A> |
| </LI> |
| </UL> |
| </SPAN> |
| </TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3"> |
| <TABLE id="PageHeader" border="0" width="100%"> |
| <TR> |
| <TD> |
| <A href="http://openejb.org/"> |
| <IMG hspace="0" src="http://openejb.apache.org/images/logo_openejb.gif" vspace="0"> |
| </A> |
| </TD> |
| <TD align="right"> |
| <A href="http://www.apache.org/"> |
| <IMG src="http://www.apache.org/images/asf-logo.gif" width="258" height="66"> |
| </A> |
| </TD> |
| </TR> |
| <TR> |
| <TD id="page_title"> |
| <!-- $TITLE --> |
| Ejb Injection Example |
| </TD> |
| |
| <TD align="right"> |
| <BR><BR> |
| <!-- Google CSE Search Box Begins --> |
| <FORM id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse"> |
| <INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90"> |
| <INPUT type="hidden" name="cof" value="FORID:0"> |
| <INPUT name="q" type="text" size="25"> |
| <INPUT type="submit" name="sa" value="Search"> |
| </FORM> |
| <SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT> |
| <!-- Google CSE Search Box Ends --> |
| |
| </TD> |
| </TR> |
| </TABLE> |
| <P> |
| <!-- $BODY --> |
| <DIV id="PageContent"> |
| <H1><A name="EjbInjectionExample-Overview"></A>Overview</H1> |
| |
| <P>This example shows how to use the @EJB annotation on a bean class to refer to other beans.</P> |
| |
| <P>This functionality is often referred as dependency injection (see |
| http://www.martinfowler.com/articles/injection.html), and has been recently introduced in |
| Java EE 5.</P> |
| |
| <P>In this particular example, we will create two session stateless beans</P> |
| |
| <UL> |
| <LI>a DataStore session bean</LI> |
| <LI>a DataReader session bean</LI> |
| </UL> |
| |
| |
| <P>The DataReader bean uses the DataStore to retrieve some informations, and |
| we will see how we can, inside the DataReader bean, get a reference to the |
| DataStore bean using the @EJB annotation, thus avoiding the use of the |
| JNDI API.</P> |
| |
| |
| <H1><A name="EjbInjectionExample-TheCode"></A>The Code</H1> |
| |
| <P>In this example we develop two simple session stateless beans (DataReader and DataStore), and show how we can use the @EJB annotation in one of these beans to get the reference to the other session bean</P> |
| |
| <H4><A name="EjbInjectionExample-DataStoresessionbean"></A>DataStore session bean</H4> |
| |
| <H5><A name="EjbInjectionExample-Localbusinessinterface"></A>Local business interface</H5> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Local |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> DataStoreLocal { |
| |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> getData(); |
| |
| }</PRE> |
| </DIV></DIV> |
| |
| <H5><A name="EjbInjectionExample-Remotebusinessinterface"></A>Remote business interface</H5> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Remote |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> DataStoreRemote { |
| |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> getData(); |
| |
| }</PRE> |
| </DIV></DIV> |
| |
| <H5><A name="EjbInjectionExample-Bean"></A>Bean</H5> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Stateless |
| <SPAN class="code-keyword">public</SPAN> class DataStoreImpl <SPAN class="code-keyword">implements</SPAN> DataStoreLocal, DataStoreRemote{ |
| |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> getData() { |
| <SPAN class="code-keyword">return</SPAN> <SPAN class="code-quote">"42"</SPAN>; |
| } |
| |
| }</PRE> |
| </DIV></DIV> |
| |
| <H4><A name="EjbInjectionExample-DataReadersessionbean"></A>DataReader session bean</H4> |
| |
| <H5><A name="EjbInjectionExample-Localbusinessinterface"></A>Local business interface</H5> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Local |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> DataReaderLocal { |
| |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> readDataFromLocalStore(); |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> readDataFromRemoteStore(); |
| }</PRE> |
| </DIV></DIV> |
| |
| <H5><A name="EjbInjectionExample-Bean"></A>Bean</H5> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Stateless |
| <SPAN class="code-keyword">public</SPAN> class DataReaderImpl <SPAN class="code-keyword">implements</SPAN> DataReaderLocal, DataReaderRemote { |
| |
| @EJB <SPAN class="code-keyword">private</SPAN> DataStoreRemote dataStoreRemote; |
| @EJB <SPAN class="code-keyword">private</SPAN> DataStoreLocal dataStoreLocal; |
| |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> readDataFromLocalStore() { |
| <SPAN class="code-keyword">return</SPAN> <SPAN class="code-quote">"LOCAL:"</SPAN>+dataStoreLocal.getData(); |
| } |
| |
| <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> readDataFromRemoteStore() { |
| <SPAN class="code-keyword">return</SPAN> <SPAN class="code-quote">"REMOTE:"</SPAN>+dataStoreRemote.getData(); |
| } |
| }</PRE> |
| </DIV></DIV> |
| |
| <P>(The remote business interface is not shown for the sake of brevity).</P> |
| |
| <TABLE cellpadding="5" width="85%" cellspacing="8px" class="tipMacro" border="0" align="center"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cwiki.apache.org/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B class="strong">@EJB annotation</B><BR> |
| <P>You can use the @EJB annotation to refer a session bean in other components specified by the Java EE platform, for instance:</P> |
| <UL> |
| <LI>Servlets</LI> |
| <LI>ServletContextListeners</LI> |
| <LI>Servlet Filters</LI> |
| <LI>JSF managed beans</LI> |
| <LI>EJB interceptors</LI> |
| <LI>JAX-WS service endpoints</LI> |
| </UL> |
| </TD></TR></TABLE> |
| |
| <H1><A name="EjbInjectionExample-Writingaunittestfortheexample"></A>Writing a unit test for the example</H1> |
| |
| <P>Writing an unit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods</P> |
| |
| <H4><A name="EjbInjectionExample-setUp"></A>setUp</H4> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java"><SPAN class="code-keyword">private</SPAN> InitialContext initialContext; |
| |
| <SPAN class="code-keyword">protected</SPAN> void setUp() <SPAN class="code-keyword">throws</SPAN> Exception { |
| Properties properties = <SPAN class="code-keyword">new</SPAN> Properties(); |
| properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, <SPAN class="code-quote">"org.apache.openejb.client.LocalInitialContextFactory"</SPAN>); |
| |
| initialContext = <SPAN class="code-keyword">new</SPAN> InitialContext(properties); |
| }</PRE> |
| </DIV></DIV> |
| |
| <H4><A name="EjbInjectionExample-Test"></A>Test</H4> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java"><SPAN class="code-keyword">public</SPAN> void testViaLocalInterface() <SPAN class="code-keyword">throws</SPAN> Exception { |
| <SPAN class="code-object">Object</SPAN> object = initialContext.lookup(<SPAN class="code-quote">"DataReaderImplLocal"</SPAN>); |
| |
| assertNotNull(object); |
| assertEquals(LOCAL_STORE_RESULT, ((DataReaderLocal)object).readDataFromLocalStore()); |
| assertEquals(REMOTE_STORE_RESULT, ((DataReaderLocal)object).readDataFromRemoteStore()); |
| }</PRE> |
| </DIV></DIV> |
| |
| <H1><A name="EjbInjectionExample-Running"></A>Running</H1> |
| |
| <P>Running the example is fairly simple, just run:</P> |
| |
| <P>$ cd injection-of-ejb<BR> |
| $ mvn clean install</P> |
| |
| <P>Which should create output like the following.</P> |
| |
| <DIV class="preformatted"><DIV class="preformattedContent"> |
| <PRE>------------------------------------------------------- |
| T E S T S |
| ------------------------------------------------------- |
| [OPENEJB:surefire] Running org.apache.openejb.examples.injection.EjbDependencyTest |
| log4j:WARN No appenders could be found for logger (OpenEJB). |
| log4j:WARN Please initialize the log4j system properly. |
| Apache OpenEJB 3.0-incubating-SNAPSHOT build: 20070105-12:45 |
| http://incubator.apache.org/openejb |
| OpenEJB ready. |
| [OPENEJB:surefire] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 2.006 sec |
| [OPENEJB:INFO] [jar:jar] |
| [OPENEJB:INFO] Building jar: openejb3/examples/injection-of-ejbs/target/ejb-injection-sample-1.0-SNAPSHOT.jar |
| [OPENEJB:INFO] [install:install] |
| [OPENEJB:INFO] Installing openejb3/examples/injection-of-ejbs/target/ejb-injection-sample-1.0-SNAPSHOT.jar to ... |
| [OPENEJB:INFO] ------------------------------------------------------------------------ |
| [OPENEJB:INFO] BUILD SUCCESSFUL |
| [OPENEJB:INFO] ------------------------------------------------------------------------ |
| |
| </PRE> |
| </DIV></DIV> |
| |
| |
| </DIV> |
| </P> |
| </TD> |
| <TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> |
| |
| |
| </TD> |
| </TR> |
| <TR class="Row5"> |
| <TD class="Col1"> </TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3"> |
| <BR> |
| <BR> |
| <IMG width="100%" height="1" src="http://openejb.apache.org/images/line_light.gif"> |
| <TABLE width="100%"> |
| <TR> |
| <TD> |
| <SPAN class="bodyGrey"> |
| <SMALL> |
| <NOTICE><!-- $FOOTER --> |
| Apache OpenEJB is an project of The Apache Software Foundation (ASF) |
| </NOTICE> |
| <BR> |
| Site Powered by |
| <A href="http://atlassian.com/">Atlassian</A> |
| <A href="http://atlassian.com/confluence/">Confluence</A> |
| . |
| </SMALL> |
| </SPAN> |
| </TD> |
| <TD align="right"> |
| <A style="color:#999;font-size:small;font-weight:normal;" href="http://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJBx30&title=Ejb%20Injection%20Example">[ edit ]</A> |
| </TD> |
| </TR> |
| </TABLE> |
| <BR> |
| </TD> |
| <TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> </TD> |
| </TR> |
| </TABLE> |
| |
| <!-- Needed for composition plugin --> |
| <!-- delay the loading of large javascript files to the end so that they don't interfere with the loading of page content --> |
| <SPAN style="display: none"> |
| <SCRIPT type="text/javascript" language="JavaScript" src="http://cwiki.apache.org/confluence/labels-javascript"></SCRIPT> |
| |
| <SCRIPT src="http://www.google-analytics.com/urchin.js" type="text/javascript"> |
| </SCRIPT> |
| <SCRIPT type="text/javascript"> |
| _uacct = "UA-2717626-1"; |
| urchinTracker(); |
| </SCRIPT> |
| </SPAN> |
| |
| </BODY> |
| </HTML> |