EJB入門例子

來源:互聯網
上載者:User

說明:本文從一個執行個體說明EJB的基本理念

1. 簡單說一下EJB概念

引用幾句比較官方的話來定義EJB

    "EJB(Enterprise JavaBeans) is a server-side component framework that simplifies the process of building enterprise-class distributed component applications in java. By using EJB, you can write scalable, reliable, secure applications without writing your own complex distibuted component framework."  

這句話主要說明EJB是伺服器端的組件架構,用來構建分布式應用;

 

    "EJB is a standard for developing and deploying server-side distributed components in Java. It defines an agreement (contract) between components and application servers that enables any component to run in any compliant application server."

這句話定義了EJB。

 

看一個圖說明EJB的分類



 2. EJB運行環境JBoss安裝

下載4.2版本以後,我這裡就用4.2.1因為工作中用的就是這個版本,下載連結http://www.jboss.org/jbossas/downloads/

下載完解壓,我將其解壓到C盤根目錄,解壓完成配置JBoss Home,如下圖:



 單機C:\jboss-4.2.1.GA\bin下run.bat可以啟動JBoss,如果啟動過程不拋出異常,說明JBoss啟動正常。

 

3.安裝Ant

下載http://ant.apache.org/下載完我同樣解壓到C盤根目錄,配置Ant Home,如下圖



 將路徑C:\apache-ant-1.8.1\bin添加到系統path.

 

4. 開發只實現Remote介面的無狀態Session Bean

Step1: Eclipse下建立一個JavaProject com.ejb.helloworld;

Step2: 定義一個包括業務方法的介面。調用EJB的用戶端只需要使用這個介面變可以調用伺服器內部這個介面的實現,介面代碼:

  Java代碼    package com.ejb.helloworld;      public interface HelloWorld {       public StringsayHello(String name);   }  

 Step3:編寫Bean Class,此類是上述介面在伺服器端的實現,代碼

  Java代碼    package com.ejb.helloworld.impl;      import javax.ejb.Remote;   import javax.ejb.Stateless;      import com.ejb.helloworld.HelloWorld;      <strong>@Stateless   @Remote ({HelloWorld.class})</strong>   public class HelloWorldBean implements HelloWorld {          public StringsayHello(String name) {            return name + " saying Hello World Loudly...";          }   }  

 注意加粗兩行代表該Bean定義EJB規範。

 Step4:Ant將上述工程打包,發布到JBoss。

在com.ejb.helloworld根目錄下構建build.xml,XML內容: Xml代碼    <?xml version="1.0"?>      <project name="com.ejb.helloworld" default="deploy" basedir="..">          <property environment="env" />       <property name="app.dir" value="${basedir}\com.ejb.helloworld" />       <property name="src.dir" value="${app.dir}\src" />       <property name="jboss.home" value="${env.JBOSS_HOME}" />       <property name="jboss.server.config" value="default" />       <property name="build.dir" value="${app.dir}\build" />       <property name="build.classes.dir" value="${build.dir}\classes" />          <path id="build.classpath">           <fileset dir="${jboss.home}\client">               <include name="*.jar" />                     </fileset>           <pathelement location="${build.classes.dir}" />       </path>          <target name="prepare" depends="clean">           <mkdir dir="${build.dir}" />           <mkdir dir="${build.classes.dir}" />       </target>          <target name="compile" depends="prepare" description="compile"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.