| <!doctype html> |
| <html class="no-js" lang="en" dir="ltr"> |
| <head> |
| <meta charset="utf-8"> |
| <meta http-equiv="x-ua-compatible" content="ie=edge"> |
| <title>Getting Started with Contexts and Dependency Injection and JSF 2.x</title> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <meta name="description" content="Getting Started with Contexts and Dependency Injection and JSF 2.x - Apache NetBeans"> |
| <meta name="author" content="Apache NetBeans"> |
| <meta name="description" content="Getting Started with Contexts and Dependency Injection and JSF 2.x - Apache NetBeans"> |
| <meta name="keywords" content="Apache NetBeans, Tutorials, Getting Started with Contexts and Dependency Injection and JSF 2.x"> |
| <meta name="generator" content="Apache NetBeans"> |
| <link rel="stylesheet" href="../../../../../../_/css/font-awesome.min.css"> |
| <link rel="alternate" type="application/atom+xml" title="Apache NetBeans Blog" href="https://netbeans.apache.org/blogs/atom" /> |
| <link rel="stylesheet" href="../../../../../../_/css/highlightjs/default.min.css"> |
| <link rel="stylesheet" href="../../../../../../_/css/netbeans.css"> |
| <link rel="apple-touch-icon" sizes="180x180" href="../../../../../../_/images/fav/apple-touch-icon.png"> |
| <link rel="icon" type="image/png" sizes="32x32" href="../../../../../../_/images/fav/favicon-32x32.png"> |
| <link rel="icon" type="image/png" sizes="16x16" href="../../../../../../_/images/fav/favicon-16x16.png"> |
| <link rel="manifest" href="../../../../../../_/images/fav/site.webmanifest"> |
| <link rel="mask-icon" href="../../../../../../_/images/fav/safari-pinned-tab.svg" color="#5bbad5"> |
| <meta name="msapplication-TileColor" content="#ffc40d"> |
| <meta name="theme-color" content="#ffffff"> |
| <link href="../../../../../../_/css/font-open-sans.css" rel="stylesheet"> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| --> |
| </head> |
| <body> |
| <div class="title-bar" data-responsive-toggle="responsive-menu" data-hide-for="medium"> |
| <button type="button" data-toggle="responsive-menu"><i style='font-size: 32px; color: #fff; padding: 8px' class='fa fa-bars'></i></button> |
| <div class="title-bar-title">Apache NetBeans</div> |
| </div> |
| <div class="top-bar" id="responsive-menu"> |
| <div class='top-bar-left'> |
| <a class='title' href="../../../../../../index.html"><img src='../../../../../../_/images/apache-netbeans.svg' style='padding: 8px; height: 48px;'> Apache NetBeans</a> |
| </div> |
| <div class="top-bar-right"> |
| <ul class="vertical medium-horizontal menu" data-responsive-menu="drilldown medium-dropdown"> |
| <li> <input id="search-input" type="text" placeholder="Search the docs"> </li> |
| <li> <a href="../../../../../../front/main/community">Community</a> </li> |
| <li> <a href="../../../../../../front/main/participate">Participate</a> </li> |
| <li> <a href="../../../../../../front/main/blogs">Blog</a></li> |
| <li> <a href="../../../../../../front/main/help">Get Help</a> </li> |
| <li> <a href="https://plugins.netbeans.apache.org/">Plugins</a> </li> |
| <li> <a href="../../../../../../front/main/download">Download</a> </li> |
| </ul> |
| </div> |
| </div> |
| |
| <!-- src/templates/news --> |
| <section class="hero news alternate"> |
| <div class='grid-container'> |
| <div class='cell'> |
| <div class="annotation">Latest release</div> |
| <h1>Apache NetBeans 28</h1> |
| <p><a class="button success" href="../../../../../../front/main/download/nb28">Download</a></p> |
| </div> |
| </div> |
| </section> |
| <div class='grid-container main-content tutorial'> |
| <article class="doc"> |
| <h1 class="sect0">Getting Started with Contexts and Dependency Injection and JSF 2.x</h1> |
| <div class="sectionbody"> |
| <div class="admonitionblock note"> |
| <table> |
| <tbody><tr> |
| <td class="icon"><i class="fa icon-note" title="Note"></i></td> |
| <td class="content">This tutorial needs a review. |
| You can <a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/javaee/cdi-intro.adoc" title="Edit this tutorial in github">edit it in GitHub </a> |
| following these <a href="../../../../../../tutorial/main/kb/docs/contributing">contribution guidelines.</a></td> |
| </tr></tbody> |
| </table> |
| </div> |
| </div> |
| <div id="toc" class="toc"> |
| <div id="toctitle"></div> |
| <ul class="sectlevel1"> |
| <li><a href="#_contexts_and_dependency_injection">Contexts and Dependency Injection</a></li> |
| <li><a href="#creating">Creating a Java Web Project with CDI Support</a></li> |
| <li><a href="#named">Accessing Beans from JSF’s Expression Language</a> |
| <ul class="sectlevel2"> |
| <li><a href="#_whats_going_on">What’s Going On?</a></li> |
| </ul> |
| </li> |
| <li><a href="#upgrading">Upgrading to an EJB</a> |
| <ul class="sectlevel2"> |
| <li><a href="#_different_ejb_types">Different EJB types</a></li> |
| </ul> |
| </li> |
| <li><a href="#seealso">See Also</a> |
| <ul class="sectlevel2"> |
| <li><a href="#_contexts_and_dependency_injection_2">Contexts and Dependency Injection</a></li> |
| <li><a href="#_javaserver_faces_2_0">JavaServer Faces 2.0</a></li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| <div id="preamble"> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p><em>Contributed by Andy Gibson</em></p> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="_contexts_and_dependency_injection"><a class="anchor" href="#_contexts_and_dependency_injection"></a>Contexts and Dependency Injection</h2> |
| <div class="sectionbody"> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p><strong>Getting Started with CDI and JSF 2.0</strong></p> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="#creating">Creating a Java Web Project with CDI Support</a></p> |
| </li> |
| <li> |
| <p><a href="#named">Accessing Beans from JSF’s Expression Language</a></p> |
| </li> |
| <li> |
| <p><a href="#upgrading">Upgrading to an EJB</a></p> |
| </li> |
| <li> |
| <p><a href="#seealso">See Also</a></p> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p><a href="../cdi-inject/" class="xref page">Working with Injection and Qualifiers in CDI</a></p> |
| </li> |
| <li> |
| <p><a href="../cdi-validate/" class="xref page">Applying @Alternative Beans and Lifecycle Annotations</a></p> |
| </li> |
| <li> |
| <p><a href="../cdi-events/" class="xref page">Working with Events in CDI</a></p> |
| </li> |
| </ol> |
| </div> |
| </li> |
| </ul> |
| </div> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>Contexts and Dependency Injection (CDI), specified by <a href="http://jcp.org/en/jsr/detail?id=299">JSR-299</a>, is an integral part of Java EE 6 and provides an architecture that allows Java EE components such as servlets, enterprise beans, and JavaBeans to exist within the lifecycle of an application with well-defined scopes. In addition, CDI services allow Java EE components such as EJB session beans and JavaServer Faces (JSF) managed beans to be injected and to interact in a loosely coupled way by firing and observing events.</p> |
| </div> |
| <div class="paragraph"> |
| <p>This tutorial is based on the blog post by Andy Gibson, entitled <a href="http://www.andygibson.net/blog/index.php/2009/12/16/getting-started-with-jsf-2-0-and-cdi-in-jee-6-part-1/">Getting Started with JSF 2.0 and CDI in JEE 6</a>. It demonstrates how you can use the IDE to set up a Java web project with support for JSF 2.0 and CDI. It continues by showing how to wire CDI managed beans with Facelets pages, and concludes with a brief example of CDI integration with EJB technology.</p> |
| </div> |
| <div class="paragraph"> |
| <p>NetBeans IDE provides built-in support for Contexts and Dependency Injection, including the option of generating the <code>beans.xml</code> CDI configuration file upon project creation, editor and navigation support for annotations, as well as various wizards for creating commonly used CDI artifacts.</p> |
| </div> |
| <div class="paragraph"> |
| <p>To complete this tutorial, you need the following software and resources.</p> |
| </div> |
| <table class="tableblock frame-all grid-all stretch"> |
| <colgroup> |
| <col style="width: 50%;"> |
| <col style="width: 50%;"> |
| </colgroup> |
| <thead> |
| <tr> |
| <th class="tableblock halign-left valign-top">Software or Resource</th> |
| <th class="tableblock halign-left valign-top">Version Required</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="../../../../../../front/main/download/" class="xref page">NetBeans IDE</a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">7.2, 7.3, 7.4, 8.0, Java EE bundle</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">version 7 or 8</p></td> |
| </tr> |
| <tr> |
| <td class="tableblock halign-left valign-top"><p class="tableblock"><a href="http://glassfish.dev.java.net/">GlassFish server</a></p></td> |
| <td class="tableblock halign-left valign-top"><p class="tableblock">Open Source Edition 3.x or 4.x</p></td> |
| </tr> |
| </tbody> |
| </table> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-note" title="Note"></i> |
| </td> |
| <td class="content"> |
| The NetBeans IDE Java bundle also includes the GlassFish Server Open Source Edition, which is a Java EE-compliant container. |
| </td> |
| </tr> |
| </table> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="creating"><a class="anchor" href="#creating"></a>Creating a Java Web Project with CDI Support</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>In this exercise you create a JSF 2.x-enabled Java web project with CDI support.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Click the New Project ( images:./new-project-btn.png[] ) button in the IDE’s main toolbar (Ctrl-Shift-N; ⌘-Shift-N on Mac).</p> |
| </li> |
| <li> |
| <p>In the New Project wizard, select the Java Web category, then select Web Application. Click Next.</p> |
| </li> |
| <li> |
| <p>Type <code>cdiDemo</code> for the project name and set the project location. Click Next.</p> |
| </li> |
| <li> |
| <p>Set the server to GlassFish server.</p> |
| </li> |
| <li> |
| <p>Set the Java EE Version to Java EE 6 Web or Java EE 7 Web.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-note" title="Note"></i> |
| </td> |
| <td class="content"> |
| The Java EE version that you select determines the CDI version that is enabled for your application and there are some important differences between CDI 1.0 and CDI 1.1. |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p>If you specify Java EE 6 Web as the Java EE version, confirm that the 'Enable Contexts and Dependency Injection' option is selected. The 'Enable Contexts and Dependency Injection' option, when selected, generates a <code>beans.xml</code> file in the project’s <code>WEB-INF</code> folder when the project template is created. The <code>beans.xml</code> file is used by CDI to instruct the Java EE-compliant server that the project is a module containing CDI beans. Java EE 6 Web supports CDI 1.0 and the generated <code>beans.xml</code> file specifies CDI 1.0 as the version.</p> |
| </li> |
| <li> |
| <p>If you specify Java EE 7 Web as the Java EE version, CDI 1.1 is enabled by default and the <code>beans.xml</code> file is not required. In Java EE 7, when no <code>beans.xml</code> is present the archive that is deployed is an <strong>implicit bean archive</strong>. If you use the New File wizard in the IDE to manually generate a <code>beans.xml</code> file in a Java EE 7 Web application, by default the deployed archive will become an <strong>explicit bean archive</strong> because the <code>beans.xml</code> file specifies CDI 1.1 as the version and also sets the attribute of the <code>bean-discovery-mode</code> to <code>all</code> .</p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p>For more details on the types of CDI archives, see <a href="http://docs.oracle.com/javaee/7/tutorial/doc/cdi-adv001.htm">Packaging CDI Applications</a> in the Java EE 7 Tutorial.</p> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/javaee/new-web-application1.png" alt="new web application1"> |
| </div> |
| <div class="title">Figure 1. The CDI option, when selected, generates a beans.xml file for the project</div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Click Next.</p> |
| </li> |
| <li> |
| <p>In the Frameworks panel, select the JavaServer Faces option.</p> |
| </li> |
| <li> |
| <p>Click the Configuration tab and confirm that Facelets is selected as the Preferred Page Language. Click Finish.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>When you click Finish, the IDE generates the web application project and opens the <code>index.xhtml</code> welcome page in the editor.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>In the Projects window, expand the Libraries > GlassFish Server node you can see that the <code>weld-osgi-bundle.jar</code> library was automatically added. The GlassFish Server includes Weld, which is JBoss' implementation of the JSR-299 CDI specification.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/javaee/projects-window1.png" alt="projects window1"> |
| </div> |
| <div class="title">Figure 2. New project contains CDI’s beans.xml file and the GlassFish library includes the Weld JAR file</div> |
| </div> |
| <div class="paragraph"> |
| <p>If you specified Java EE 6 Web as the Java EE version when you created the project, note that the Web Pages > WEB-INF folder includes a <code>beans.xml</code> file. This file is currently empty, but can be used to specify bean related information in XML as an alternative to annotations.</p> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="named"><a class="anchor" href="#named"></a>Accessing Beans from JSF’s Expression Language</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>This exercise demonstrates how you can wire CDI managed beans to Facelets pages using EL syntax.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>In the Projects window, right-click the Source Packages node and choose New > Java Class.</p> |
| </li> |
| <li> |
| <p>In the New Java Class wizard, name the class <strong>MessageServerBean</strong>, and type in <strong>exercise1</strong> as the Package. (The new package is created upon completing the wizard.) Click Finish.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/javaee/new-java-class.png" alt="new java class"> |
| </div> |
| <div class="title">Figure 3. Create new Java classes using the Java Class wizard</div> |
| </div> |
| <div class="paragraph"> |
| <p>The new package and class are generated, and the class opens in the editor.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Annotate the class with the <code>@Named</code> and <code>@Dependent</code> annotations, and create a single method to return a string.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">package exercise1; |
| |
| *import javax.enterprise.context.Dependent; |
| import javax.inject.Named;* |
| |
| *@Dependent |
| @Named* |
| public class MessageServerBean { |
| |
| *public String getMessage() { |
| return "Hello World!"; |
| }* |
| }</code></pre> |
| </div> |
| </div> |
| <div class="paragraph"> |
| <p>As you type the <code>@Dependent</code> and <code>@Named</code> annotation, press Ctrl-Space to invoke the editor’s code completion support, as well as Javadoc documentation. If you apply the annotation using the editor’s code completion facilities (i.e., select the appropriate annotation and press Enter), the <code>import</code> statement is automatically added to the file. In the Javadoc pop-up, you can also click the 'Show documentation in external web browser' ( images:./external-web-browser-btn.png[] ) button to display the full-sized Javadoc in a separate window.</p> |
| </div> |
| <div class="admonitionblock note"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-note" title="Note"></i> |
| </td> |
| <td class="content"> |
| The <code>@Dependent</code> annotation defines the scope of the managed bean. In an <strong>implicit bean archive</strong> a managed bean is only discoverable and can only be managed by the container when a scope is specified. The application in this tutorial will be packaged as an implicit bean archive if you specified Java EE 7 Web as the Java EE version when you created the project and you did not create <code>beans.xml</code> . For details on specifying the scope of managed beans, see <a href="http://docs.oracle.com/javaee/7/tutorial/doc/jsf-configure001.htm">Using Annotations to Configure Managed Beans</a> in the Java EE 7 Tutorial. |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Save the file (Ctrl-S; ⌘-S on Mac). By adding the <code>@Named</code> annotation, the <code>MessageServerBean</code> class becomes a <em>managed bean</em>, as defined by CDI.</p> |
| </li> |
| <li> |
| <p>Switch to the <code>index.xhtml</code> Facelets page (press Ctrl-Tab) in the editor, and add the following content to the <code><h:body></code> tags.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><h:body> |
| Hello from Facelets |
| *<br/> |
| Message is: #{messageServerBean.message} |
| <br/> |
| Message Server Bean is: #{messageServerBean}* |
| </h:body></code></pre> |
| </div> |
| </div> |
| <div class="admonitionblock tip"> |
| <table> |
| <tr> |
| <td class="icon"> |
| <i class="fa icon-tip" title="Tip"></i> |
| </td> |
| <td class="content"> |
| You can press Ctrl-Space in EL expressions to take advantage of code completion suggestions. The editor’s code completion lists managed beans and their properties. Because the <code>@Named</code> annotation transforms the <code>MessageServerBean</code> class into a CDI managed bean, it becomes accessible within EL syntax, just as though it were a JSF managed bean.# |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/javaee/facelets-el-completion.png" alt="facelets el completion"> |
| </div> |
| <div class="title">Figure 4. Create new Java classes using the Java Class wizard</div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Click the Run Project ( images:./run-project-btn.png[] ) button in the IDE’s main toolbar. The project is compiled and deployed to GlassFish, and the application welcome page (<code>index.xhtml</code>) opens in the browser. You can see the "Hello World!" message from the <code>MessageServerBean</code> displayed on the page.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/javaee/browser-output1.png" alt="browser output1"> |
| </div> |
| <div class="title">Figure 5. Application welcome page displays MessageServerBean details</div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Go back into the message bean and change the message to something else (e.g., "Hello Weld!"). Save the file (Ctrl-S; ⌘-S on Mac), then refresh the browser. The new message automatically appears. Thanks to the IDE’s Deploy on Save feature, any changes you save result in automatic compilation and redeployment to the server.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="paragraph"> |
| <p>From the third line in the page you can see that the class name is <code>exercise1.MessageServerBean</code>. Note that the bean is just a POJO (Plain Old Java Object). Even though you are developing in Java EE, there is no complex class hierarchy wrapped in layers of transactions, interceptors and all that "heavy" stuff you keep hearing about.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="_whats_going_on"><a class="anchor" href="#_whats_going_on"></a>What’s Going On?</h3> |
| <div class="paragraph"> |
| <p>When the application is deployed the server looks for CDI managed beans. In a Java EE 7 application, the classes on the path are scanned for CDI annotations by default. In a Java EE 6 application, the classes are scanned for CDI annotations if the module contains a <code>beans.xml</code> file. In a CDI module, all beans are registered with Weld, and the <code>@Named</code> annotation is used to match beans to injection points. When the <code>index.xhtml</code> page was rendered, JSF tried to resolve the value of <code>messageServerBean</code> in the page using the registered expression resolvers in JSF. One of these is the Weld EL Resolver which has the <code>MessageServerBean</code> class registered under the name <code>messageServerBean</code>. We could have specified a different name with the <code>@Named</code> annotation, but since we did not it was registered under the default name, being the class name with a lower-case first letter. The Weld resolver returns an instance of this bean in response to the request from JSF. Bean naming is only needed when using EL expressions and should not be used as a mechanism for injection since CDI provides type-safe injection by class type and qualifier annotations.</p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="upgrading"><a class="anchor" href="#upgrading"></a>Upgrading to an EJB</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>As we are using a Java EE stack, we can easily deploy our bean as an EJB with some small changes thanks to EJB 3.1.</p> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Open the <code>MessageServerBean</code> and add the <code>javax.ejb.Stateless</code> annotation at the class level and change the string to 'Hello EJB!'.</p> |
| </li> |
| </ol> |
| </div> |
| <div class="listingblock"> |
| <div class="content"> |
| <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">package exercise1; |
| |
| *import javax.ejb.Stateless;* |
| import javax.enterprise.context.Dependent; |
| import javax.inject.Named; |
| |
| /** |
| * |
| * @author nbuser |
| */ |
| @Dependent |
| @Named |
| *@Stateless* |
| public class MessageServerBean { |
| |
| public String getMessage() { |
| return "*Hello EJB!*"; |
| } |
| }</code></pre> |
| </div> |
| </div> |
| <div class="olist arabic"> |
| <ol class="arabic"> |
| <li> |
| <p>Save the file (Ctrl-S; ⌘-S on Mac), then go to your browser and refresh. You will see output similar to the following:</p> |
| </li> |
| </ol> |
| </div> |
| <div class="imageblock"> |
| <div class="content"> |
| <img src="../../../../_images/kb/docs/javaee/browser-output-ejb1.png" alt="browser output ejb1"> |
| </div> |
| <div class="title">Figure 6. Using the @Stateless annotation transforms MessageServerBean into an EJB</div> |
| </div> |
| <div class="paragraph"> |
| <p>Amazingly, we turned our POJO into a fully-featured EJB with just one annotation. We saved changes then refreshed the page, and our changes appeared. In doing so it wasn’t necessary to create any weird project configurations, local interfaces or arcane deployment descriptors.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="_different_ejb_types"><a class="anchor" href="#_different_ejb_types"></a>Different EJB types</h3> |
| <div class="paragraph"> |
| <p>You can also try using the <code>@Stateful</code> annotation. Alternatively, you could try the new <code>@Singleton</code> annotation for singleton instances. If you do, you may notice that there is are two annotations: <code>javax.ejb.Singleton</code> and <code>javax.inject.Singleton</code>. Why two singletons? The CDI singleton (<code>javax.inject.Singleton</code>) lets you define a singleton instance outside of EJB in case you are using CDI in a non-EJB environment. The EJB singleton (<code>javax.ejb.Singleton</code>) provides all the features of an EJB such as transaction management. Therefore, you have the choice depending on your needs and whether you are working in an EJB environment.</p> |
| </div> |
| <div class="paragraph"> |
| <p><a href="../../../../../../front/main/community/mailing-lists/" class="xref page">Send Feedback on This Tutorial</a></p> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="sect1"> |
| <h2 id="seealso"><a class="anchor" href="#seealso"></a>See Also</h2> |
| <div class="sectionbody"> |
| <div class="paragraph"> |
| <p>The next installment of this series focuses on CDI injection, and provides a closer look at using CDI for managing dependencies in a Java EE environment.</p> |
| </div> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="../cdi-inject/" class="xref page">Working with Injection and Qualifiers in CDI</a></p> |
| </li> |
| </ul> |
| </div> |
| <div class="paragraph"> |
| <p>For more information about CDI and JSF 2.0, see the following resources.</p> |
| </div> |
| <div class="sect2"> |
| <h3 id="_contexts_and_dependency_injection_2"><a class="anchor" href="#_contexts_and_dependency_injection_2"></a>Contexts and Dependency Injection</h3> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="../cdi-validate/" class="xref page">Applying @Alternative Beans and Lifecycle Annotations</a></p> |
| </li> |
| <li> |
| <p><a href="../cdi-events/" class="xref page">Working with Events in CDI</a></p> |
| </li> |
| <li> |
| <p><a href="http://blogs.oracle.com/enterprisetechtips/entry/using_cdi_and_dependency_injection">Enterprise Tech Tip: Using CDI and Dependency Injection for Java in a JSF 2.0 Application</a></p> |
| </li> |
| <li> |
| <p><a href="http://docs.oracle.com/javaee/7/tutorial/doc/cdi-basic.htm">The Java EE 6 Tutorial: Introduction to Contexts and Dependency Injection for Java EE</a></p> |
| </li> |
| <li> |
| <p><a href="http://jcp.org/en/jsr/detail?id=299">JSR 299: Specification for Contexts and Dependency Injection</a></p> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <div class="sect2"> |
| <h3 id="_javaserver_faces_2_0"><a class="anchor" href="#_javaserver_faces_2_0"></a>JavaServer Faces 2.0</h3> |
| <div class="ulist"> |
| <ul> |
| <li> |
| <p><a href="../../web/jsf20-intro/" class="xref page">Introduction to JavaServer Faces 2.x</a></p> |
| </li> |
| <li> |
| <p><a href="../../web/jsf20-crud/" class="xref page">Generating a JavaServer Faces 2.x CRUD Application from a Database</a></p> |
| </li> |
| <li> |
| <p><a href="../../../../samples/scrum-toys/" class="xref page">Scrum Toys - The JSF 2.0 Complete Sample Application</a></p> |
| </li> |
| <li> |
| <p><a href="http://www.oracle.com/technetwork/java/javaee/javaserverfaces-139869.html">JavaServer Faces Technology</a> (Official homepage)</p> |
| </li> |
| <li> |
| <p><a href="http://docs.oracle.com/javaee/7/tutorial/doc/jsf-page.htm">The Java EE 7 Tutorial: Using JavaServer Faces Technology in Web Pages</a></p> |
| </li> |
| <li> |
| <p><a href="http://jcp.org/en/jsr/summary?id=314">JSR 314: Specification for JavaServer Faces 2.0</a></p> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </div> |
| <section class='tools'> |
| <ul class="menu align-center"> |
| <li><a title="Facebook" href="https://www.facebook.com/NetBeans"><i class="fa fa-md fa-facebook"></i></a></li> |
| <li><a title="Twitter" href="https://twitter.com/netbeans"><i class="fa fa-md fa-twitter"></i></a></li> |
| <li><a title="Github" href="https://github.com/apache/netbeans"><i class="fa fa-md fa-github"></i></a></li> |
| <li><a title="YouTube" href="https://www.youtube.com/user/netbeansvideos"><i class="fa fa-md fa-youtube"></i></a></li> |
| <li><a title="Atom Feed" href="https://netbeans.apache.org/blogs/atom"><i class="fa fa-mf fa-rss"></i></a></li> |
| <li><a title="Slack" href="https://tinyurl.com/netbeans-slack-signup/"><i class="fa fa-md fa-slack"></i></a></li> |
| <li><a title="Issues" href="https://github.com/apache/netbeans/issues"><i class="fa fa-mf fa-bug"></i></a></li> |
| </ul> |
| <ul class="menu align-center"> |
| <li><a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/javaee/cdi-intro.adoc" title="See this page in github"><i class="fa fa-md fa-edit"></i> See this page in GitHub.</a></li> |
| </ul> |
| </section> |
| </article> |
| </div> |
| <div class='grid-container incubator-area' style='margin-top: 64px'> |
| <div class='grid-x grid-padding-x'> |
| <div class='large-auto cell text-center'> |
| <a href="https://www.apache.org/"> |
| <img style="height: 60px" title="Apache Software Foundation" src="../../../../../../_/images/asf_logo_wide.svg" /> |
| </a> |
| </div> |
| <div class='large-auto cell text-center'> |
| <a href="https://www.apache.org/events/current-event.html"> |
| <img style="width:234px; height: 60px;" title="Apache Software Foundation current event" src="https://www.apache.org/events/current-event-234x60.png"/> |
| </a> |
| </div> |
| </div> |
| </div> |
| <footer> |
| <div class="grid-container"> |
| <div class="grid-x grid-padding-x"> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/about">About</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/community/who">Who's Who</a></li> |
| <li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li><a href="https://www.apache.org/security/">Security</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/community">Community</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/community/mailing-lists">Mailing lists</a></li> |
| <li><a href="../../../../../../front/main/community/committer">Becoming a committer</a></li> |
| <li><a href="../../../../../../front/main/community/events">NetBeans Events</a></li> |
| <li><a href="https://www.apache.org/events/current-event.html">Apache Events</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/participate">Participate</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/participate/submit-pr">Submitting Pull Requests</a></li> |
| <li><a href="../../../../../../front/main/participate/report-issue">Reporting Issues</a></li> |
| <li><a href="../../../../../../front/main/participate/#documentation">Improving the documentation</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/help">Get Help</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/help/#documentation">Documentation</a></li> |
| <li><a href="../../../../../../wiki/main/wiki">Wiki</a></li> |
| <li><a href="../../../../../../front/main/help/#support">Community Support</a></li> |
| <li><a href="../../../../../../front/main/help/commercial-support">Commercial Support</a></li> |
| </ul> |
| </div> |
| <div class="large-auto cell"> |
| <h1><a href="../../../../../../front/main/download">Download</a></h1> |
| <ul> |
| <li><a href="../../../../../../front/main/download">Releases</a></li> |
| <li><a href="https://plugins.netbeans.apache.org/">Plugins</a></li> |
| <li><a href="../../../../../../front/main/download/#_daily_builds_and_building_from_source">Building from source</a></li> |
| <li><a href="../../../../../../front/main/download/#_older_releases">Previous releases</a></li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </footer> |
| <div class='footer-disclaimer'> |
| <div class="footer-disclaimer-content"> |
| <p>Copyright © 2017-2025 <a href="https://www.apache.org">The Apache Software Foundation</a>.</p> |
| <p>Licensed under the Apache <a href="https://www.apache.org/licenses/">license</a>, version 2.0</p> |
| <div style='max-width: 40em; margin: 0 auto'> |
| <p>Apache, Apache NetBeans, NetBeans, the Apache feather logo and the Apache NetBeans logo are trademarks of <a href="https://www.apache.org">The Apache Software Foundation</a>.</p> |
| <p>Oracle and Java are registered trademarks of Oracle and/or its affiliates.</p> |
| <p>The Apache NetBeans website conforms to the <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Apache Software Foundation Privacy Policy</a></p> |
| </div> |
| </div> |
| </div> |
| |
| |
| <script src="../../../../../../_/js/vendor/lunr.js"></script> |
| <script src="../../../../../../_/js/search-ui.js" id="search-ui-script" data-site-root-path="../../../../../.." data-snippet-length="100" data-stylesheet="../../../../../../_/css/search.css"></script> |
| <script async src="../../../../../../search-index.js"></script> |
| <script src="../../../../../../_/js/vendor/jquery.min.js"></script> |
| <script src="../../../../../../_/js/vendor/what-input.min.js"></script> |
| <script src="../../../../../../_/js/vendor/foundation.min.js"></script> |
| <script src="../../../../../../_/js/vendor/jquery.colorbox-min.js"></script> |
| <script src="../../../../../../_/js/netbeans.js"></script> |
| <script> |
| $(function(){ $(document).foundation(); }); |
| </script> |
| <script src="../../../../../../_/js/vendor/highlight.min.js"></script> |
| <script> |
| document.addEventListener('DOMContentLoaded', (event) => { |
| document.querySelectorAll('pre code').forEach((el) => { |
| hljs.highlightElement(el); |
| }); |
| }); |
| </script> |
| </body> |
| </html> |