服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

循速渐进学用session bean(一)


  session bean的作用

  session bean代表客户应用和ejb容器之间的会话。session bean通常都是实现商业逻辑并且和entity bean交互以执行具体操作。不过session bean并不一定要使用entity bean。如果需要,它可以直接和数据库通信。

  一个"hello world"session bean

  enterprise javabean的结构在开始的时候比较难理解,不过只要你用过一下,ejb也不是太难。幸运的是,你可以无需涉及数据库连接或者是事务,就可以熟悉基本的ejb结构。要做到这一点,可以由最有名的"hello world"bean开始。

  当你在设计ejb应用的时候,你可能不知道是先由bean开始然后再创建接口,或者先由接口开始然后创建bean。我建议你最好由接口开始。因为如果你不知道客户如何使用bean,你就无需编写它。

  创建remote接口

  列表6.1展示的是helloworldsession接口,它是"hello world" session bean的remote接口。

listing 6.1 source code for helloworldsession.java
package usingj2ee.hello;

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

/** defines the methods you can call on a helloworldsession object */

public interface helloworldsession extends ejbobject
{

/** returns the session's greeting */
public string getgreeting() throws remoteexception;

/** changes the session's greeting */
public void setgreeting(string agreeting) throws remoteexception;

}

  创建home接口

  一个session bean的home接口包含有创建新session的方法。对于"hello world"例子,有两个不同的create方法,一个没有参数,而另一个允许你提供自己的欢迎词。列表6.2展示了helloworldsessionhome接口。

listing 6.2 source code for helloworldsessionhome.java
package usingj2ee.hello;

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

/** defines the methods for creating a helloworldsession */

public interface helloworldsessionhome extends ejbhome
{

/** creates a helloworldsession bean with default settings */
public helloworldsession create() throws remoteexception, createexception;

/** creates a helloworldsession bean with a specific initial greeting */
public helloworldsession create(string agreeting)
throws remoteexception, createexception;

}

创建实现的类

  接口是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。

扫描关注微信公众号