MyEclipse下EJB应用程序的开发

MyEclipse下EJB应用程序的开发 例子

版权所有,欢迎转载,转载请注明转自http://www.suneca.com

EJB(Enterprise JavaBean)是SUN服务器端组件模型,是J2EE的一部分,基于EJB技术,可以开分布式应用程序。
Jbuilder是一个非常理想的EJB开发工具,在现在这么多免费的开发工具前面,Borland还有一定的市场,可见Jbuilder还是有非常明显的优势。
MyEclipse是一个基于Eclipse的开发工具,这个开发工具也很便宜,官方报价也就29.9美元,算是非常便宜的一个工具了,现在国内很多做Java开发的程序都是使用这个工具。
EJB的开发需要有J2EE应用服务器的支持,注意:Tomcat只是一个Servlet容器,不是一个J2EE应用服务器。本次例子使用的是开源的Jboss4.x
Jboss应用服务器的下载地址: http://www.jboss.org
下面介绍如何基于MyEclipse开发无状态的SessionBean。
第一步,配置J2EE应用服务器:
点击WindowsPreferences,在弹出的对话框当中选择MyEclipse-->Jboss-->Jboss 4.x



将Jboss Sever状态改为Enable
Jboss home directory改为Jboss的安装路径
JDK改为系统的JDK路径,注意,Jboss 4使用的是Tomcat5.5内核,Tomcat5.5运行需要有JDK的支持。
最后,点击OK按钮
第二步,创建一个EJB工程:



点击Next>按钮,在Project Name当中输入EJBTraining,或者输入你喜欢的工程名。如下图所示:


在Package Explorer可以看到工程的目录:


第三步,新建一个SessionBean


点击Next>


在Package上填入zizz.ejb(一般企业做开发的时候都有一定的命名规范,一般喜欢以ejb结尾)
在Name上填入UserServiceBean(一个EJB Bean一般情况是以Bean结尾,特别是在Eclipse开发框架下,因为是使用Xdoclet来生成代码的)
在EJB类型上选择Stateless,表示无状态会话Bean
在Select the access of the EJB上面选择Remote
选上ejbCreate() method
系统生成的UserServiceBean如下:
程序代码 程序代码
package zizz .ejb ;

import java .rmi .RemoteException ;

import javax .ejb .EJBException ;
import javax .ejb .SessionBean ;
import javax .ejb .SessionContext ;

import javax .ejb .CreateException ;

/**
* XDoclet-based session bean.  The class must be declared
* public according to the EJB specification.
*
* To generate the EJB related files to this EJB:
*        - Add Standard EJB module to XDoclet project properties
*        - Customize XDoclet configuration for your appserver
*        - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="UserService"
*           display-name="Name for UserService"
*           description="Description for UserService"
*           jndi-name="ejb/UserService"
*           type="Stateless"
*           view-type="remote"
*/

public class UserServiceBean implements SessionBean {

     /** The session context */
     private SessionContext context ;

     public UserServiceBean ( ) {
         // TODO Auto-generated constructor stub
     }

     public void ejbActivate ( ) throws EJBException , RemoteException {
         // TODO Auto-generated method stub

     }

     public void ejbPassivate ( ) throws EJBException , RemoteException {
         // TODO Auto-generated method stub

     }

     public void ejbRemove ( ) throws EJBException , RemoteException {
         // TODO Auto-generated method stub

     }

     /**
     * Set the associated session context. The container calls this method
     * after the instance creation.
     *
     * The enterprise bean instance should store the reference to the context
     * object in an instance variable.
     *
     * This method is called with no transaction context.
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */

     public void setSessionContext (SessionContext newContext )
        throws EJBException {
        context = newContext ;
     }

     /**
     * An ejbCreate method as required by the EJB specification.
     *
     * The container calls the instance?s <code>ejbCreate</code> method whose
     * signature matches the signature of the <code>create</code> method invoked
     * by the client. The input parameters sent from the client are passed to
     * the <code>ejbCreate</code> method. Each session bean class must have at
     * least one <code>ejbCreate</code> method. The number and signatures
     * of a session bean?s <code>create</code> methods are specific to each
     * session bean class.
     *
     * @throws CreateException Thrown if method fails due to system-level error.
     *
     * @ejb.create-method
     *
     */

     public void ejbCreate ( ) throws CreateException {
         // TODO Add ejbCreate method implementation
     }

     /**
     * An example business method
     *
     * @ejb.interface-method view-type = "remote"
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */

     public void replaceWithRealBusinessMethod ( ) throws EJBException {
         // rename and start putting your business logic here
     }

}


将replaceWithRealBusinessMethod方法改为具体的业务方法,修改完的业务方法如下:
程序代码 程序代码
package zizz[color=#0000ff].ejb ;

import java .rmi .RemoteException ;
import java .util .ArrayList ;
import java .util .List ;

import javax .ejb .CreateException ;
import javax .ejb .EJBException ;
import javax .ejb .SessionBean ;
import javax .ejb .SessionContext ;

import zizz .model .User ;

/**
* XDoclet-based session bean.  The class must be declared
* public according to the EJB specification.
*
* To generate the EJB related files to this EJB:
*        - Add Standard EJB module to XDoclet project properties
*        - Customize XDoclet configuration for your appserver
*        - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="UserService"
*           display-name="Name for UserService"
*           description="Description for UserService"
*           jndi-name="ejb/UserService"
*           type="Stateless"
*           view-type="remote"
*/

public class UserServiceBean implements SessionBean {

     /** The session context */
     private SessionContext context ;

     public UserServiceBean ( ) {
         // TODO Auto-generated constructor stub
     }

     public void ejbActivate ( ) throws EJBException , RemoteException {
         // TODO Auto-generated method stub

     }

     public void ejbPassivate ( ) throws EJBException , RemoteException {
         // TODO Auto-generated method stub

     }

     public void ejbRemove ( ) throws EJBException , RemoteException {
         // TODO Auto-generated method stub

     }

     /**
     * Set the associated session context. The container calls this method
     * after the instance creation.
     *
     * The enterprise bean instance should store the reference to the context
     * object in an instance variable.
     *
     * This method is called with no transaction context.
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */

     public void setSessionContext (SessionContext newContext )
        throws EJBException {
        context = newContext ;
     }

     /**
     * An ejbCreate method as required by the EJB specification.
     *
     * The container calls the instance?s <code>ejbCreate</code> method whose
     * signature matches the signature of the <code>create</code> method invoked
     * by the client. The input parameters sent from the client are passed to
     * the <code>ejbCreate</code> method. Each session bean class must have at
     * least one <code>ejbCreate</code> method. The number and signatures
     * of a session bean?s <code>create</code> methods are specific to each
     * session bean class.
     *
     * @throws CreateException Thrown if method fails due to system-level error.
     *
     * @ejb.create-method
     *
     */

     public void ejbCreate ( ) throws CreateException {
         // TODO Add ejbCreate method implementation
     }

     /**
     * An example business method
     *
     * @ejb.interface-method view-type = "remote"
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */

     public List listAllUsers ( ) throws EJBException {
        List <User > users = new ArrayList <User > ( ) ;
        User user1 = new User ( ) ;
        user1 .setId (1 ) ;
        user1 .setName ( "达闻西" ) ;
        user1 .setLoginId ( "dwx" ) ;
        users . add (user1 ) ;
        
        User user2 = new User ( ) ;
        user2 .setId (2 ) ;
        user2 .setName ( "零零漆" ) ;
        user2 .setLoginId ( "007" ) ;
        users . add (user2 ) ;
         return users ;
     }

}
[/color]

User实体为:
程序代码 程序代码
package zizz .model ;

import java .io .Serializable ;

/**
*
* ZIZZ,该对象用以网络传输,必须实现串行化接口.
* @author ZIZZ.
* @version Create Date: 2007-12-5 下午05:29:10.
*/

public class User implements Serializable {
    
     /**
     * serialVersionUID
     */

     private static final long serialVersionUID = -2176183120082246628L ;

     private int id ;
    
     private String name ;
    
     private String loginId ;

     public int getId ( ) {
         return id ;
     }

     public void setId ( int id ) {
         this .id = id ;
     }

     public String getLoginId ( ) {
         return loginId ;
     }

     public void setLoginId ( String loginId ) {
         this .loginId = loginId ;
     }

     public String getName ( ) {
         return name ;
     }

     public void setName ( String name ) {
         this .name = name ;
     }
    
}


其中@ejb.interface-method view-type = "remote"这些XDolect注解不应该删除,因为一会还需要靠XDolect生成代码。

第四步,配置XDolect
点击工程右键,选择Properties


将弹出工程属性对话框,在弹出的对话框当中选择Xdolect,如下图所示


点击Add Standard按钮,在弹出的对话框当中选择Stardard EJB,最后点击OK按钮,如下图所示:


点击Standard EJB,在下面将会出来ejbdolect的选择项,将不必要的选择项目删,如下图所示:


删完后的配置项为:  


接着,点击ejbdolect,右键,选择Add


在弹出的对象框当中选择jboss,最后,点击ok按钮


接着需要配置jboss的配置,点击jboss,在加边的配置项当中做如下的修改
Version定义为:4.0
destDir定义为:src/META-INF
最后,点击OK
第五步,生成代码:
在工程上右键,选择MyEclipseRun Xdolect


工程新的目录结构:


此时,我们的EJB程序就开发得差不多了,接着来,进行发布
第五步:发布应用程序(发布跟发布应用程序一样,详细可以参考)
http://www.suneca.com/article.asp?id=18


第六步,启动Jboss应用服务器
在控制台上,可以看到这样提示信息:
程序代码 程序代码
12:44:48,375 INFO  [EjbModule] Deploying UserService
12:44:48,703 INFO  [ProxyFactory] Bound EJB Home 'UserService' to jndi 'ejb/UserService'


到此,我们的EJB组件已经发布成功了,接下来,我们需要开发一个客户端来验证一下我们的EJB组件的有效性。

我们需要对对model对象及接口(即远程接口跟本地接口)打一个包,然后将打的这个包存存放于客户端的classpath当中。
第七步,打包:
点击File-->Export,在弹出的对象框当中选择JAR file,如下图所示:

点击Next>,将弹出如下的对话框

在JAR file输入框当中输入:c:\UserServiceClient.jar,表示在c盘下生成一个UserServiceClient的jar文件。
第八步,创建一个客户端工程
将UserServiceClient.jar及jbossall-client.jar文件加入到客户端工程的classpath当中。

第九步,编程测试客户端
创建一个测试类,比如叫UserServiceClient.java,调用服务器的组件,
UserServiceClient内容如下:
程序代码 程序代码
package zizz .ejb .client ;

import java .rmi .RemoteException ;
import java .util .Iterator ;
import java .util .List ;
import java .util .Properties ;

import javax .ejb .CreateException ;
import javax .naming .Context ;
import javax .naming .InitialContext ;
import javax .naming .NamingException ;

import zizz .interfaces .UserService ;
import zizz .interfaces .UserServiceHome ;
import zizz .model .User ;

/**
*
* 该程序用于调用服务器端的组件.
*
* @author ZIZZ.
* @version Create Date: 2007-12-6 下午02:06:00.
*/

public class UserServiceClient {

     /**
     * @param args
     */

     public static void main ( String [ ] args ) {
         //设置Context工厂,不同应用服务器设置不同
        Properties env = new Properties ( ) ;
        env . setProperty (Context .INITIAL_CONTEXT_FACTORY , "org.jnp.interfaces.NamingContextFactory" ) ;
        env . setProperty (Context .URL_PKG_PREFIXES , "org.jboss.naming.client" ) ;
         // 设置jnp地址
        env . setProperty (Context .PROVIDER_URL , "jnp://localhost:1099" ) ;
        try {
             //得到Context
            Context enc = new InitialContext (env ) ;
             //得到本地接口
            UserServiceHome home = (UserServiceHome ) enc .lookup ( "ejb/UserService" ) ;
             //得到远程对象
            UserService userService = home . create ( ) ;
             //调用远程对象的方法
            List users = userService .listAllUsers ( ) ;
            Iterator iter = users .iterator ( ) ;
             while (iter .hasNext ( ) ) {
                User user = (User ) iter .next ( ) ;
                 System .out .println (user .getName ( ) ) ;
             }
         } catch (NamingException e ) {
            e .printStackTrace ( ) ;
         } catch (RemoteException e ) {
            e .printStackTrace ( ) ;
         } catch (CreateException e ) {
            e .printStackTrace ( ) ;
         }

     }

}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值