您现在的位置是: 软件 > 开发者网络 > 程序方舟 > Java专栏 > 正文


-Delphi 文件处理及打印绘图
-智能备份 -众里寻它千百度
-Flash MX 视频导入功能详解
-如何使用Ghost备份和恢复系统

循速渐进学用Session Bean(一)
2002-06-18· ·QQ新人类编译··yesky

上一页  1 2  

  创建实现的类

  接口是EJB开发中比较简单的部分,而session bean还需要更多的工作。当你写一个session bean时,有一些方法你必须包含在bean中以满足EJB容器的要求。这些额外的方法是setSessionContext, ejbRemove, ejbActivate和 ejbPassivate。此外,当你实现你的create方法时,你需要将它们命名为ejbCreate而不只是create。

  注意

  要记住的是容器调用这些方法。当使用Home接口的方法来创建一个新的EJB时,容器最终会调用ejbCreate方法。同样,当删除一个bean时,容器将会调用ejbRemove方法来告诉bean它已经被移除。

  列表6.3是HelloWorldSession和HelloWorldSessionHome接口的实现,你看一下就清楚了。

Listing 6.3 Source Code for HelloWorldSessionImpl.ava
package usingj2ee.hello;

import java.rmi.*;
import java.util.*;
import javax.ejb.*;

/** The implementation class for the HelloWorldSession bean */

public class HelloWorldSessionImpl implements SessionBean
{
/** Holds the session's greeting */
protected String greeting;

/** The session context provided by the EJB container. A session bean must
hold on to the context it is given. */

private SessionContext context;

/** An EJB must have a public, parameterless constructor */

public HelloWorldSessionImpl()
{
}

/** Called by the EJB container to set this session's context */

public void setSessionContext(SessionContext aContext)
{
context = aContext;
}

/** Called by the EJB container when a client calls the create() method in
the Home interface */

public void ejbCreate()
throws CreateException
{
greeting = "Hello World!";
}

/** Called by the EJB container when a client calls the
create(String) method in the Home interface */

public void ejbCreate(String aGreeting)
throws CreateException
{
greeting = aGreeting;
}

/** Called by the EJB container to wake this session bean up after it
has been put to sleep with the ejbPassivate method. */

public void ejbActivate()
{
}

/** Called by the EJB container to tell this session bean that it is being
suspended from use (it's being put to sleep). */

public void ejbPassivate()
{
}

/** Called by the EJB container to tell this session bean that it has been
removed, either because the client invoked the remove() method or the
container has timed the session out. */

public void ejbRemove()
{
}

/** Returns the session's greeting */

public String getGreeting()
{
return greeting;
}

/** Changes the session's greeting */

public void setGreeting(String aGreeting)
{
greeting = aGreeting;
}
}

  你想做的全部工作就是建立一个带有getGreeting和setGreeting方法的bean,你最终会得到两个Java接口和一个有8个方法的类。Enterprise JavaBeans明显需要更多的工作,对于小的项目来说,创建EJB的方法所要做的工作,和实现商业逻辑的方法的工作一样多。不过,当应用增长时,将会发现在EJB上所做的额外工作是值得的,因为只有这样做,混合对象以实现新的功能时就变得更加简单。

  集成开发环境(IDEs)对创建Enterprise JavaBeans提供一些支持。不幸的是,大多数提供这个功能的工具都是昂贵的"企业版"。Allaire (http://www.allaire.com)企业版本IDE的价格合理并且对EJB有着很好的支持。希望你不久后可找到一些免费的工具来创建EJB。

  提示

  由于在编写EJB应用时你最终会产生很多的文件,因此对于产生的类使用一些一致的命名传统是重要的。Remote和 Home接口都通常命令为XXX和XXXHome,XXX是bean的名字。实现的类通常命令为XXXBean或者XXXImpl。你还可以考虑根据这个bean是一个session bean还是一个entity bean,将实现的类命令为XXXEB或者XXXSB。不管你决定如何命名你的类,它们都必须是一致的,这样在多人开发时会避免很多麻烦。

  注意

  这里使用XXX代表Remote接口,XXXHome代表Home接口,而XXXImp1代表实现。该bean使用Remote接口名。如果Remote接口被称为ShoppingCart,Home的接口就是ShoppingCartHome,实现就是ShoppingCartImp1,而且该bean被引用为ShoppingCart bean。


上一页  1 2  

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 EJB系列教程之三
 EJB系列教程之二
 EJB系列教程之一
 提升EJB性能的12招
 使用Forte for Java开发EJB
 一步一步用JBuilder5开发EJB
 深入探讨EJB中新的消息驱动组件
 充分利用 EJB 使移动工作群体变得强大
 JavaBean与EJB有何不同
 EJB核心技术及其应用
 EJB的专用术语解释
 全面研读 EJB 2.0
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!