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 import java.rmi.*; /** defines the methods you can call on a helloworldsession object */ public interface helloworldsession extends ejbobject /** returns the session's greeting */ /** changes the session's greeting */ } |
创建home接口
一个session bean的home接口包含有创建新session的方法。对于"hello world"例子,有两个不同的create方法,一个没有参数,而另一个允许你提供自己的欢迎词。列表6.2展示了helloworldsessionhome接口。
listing 6.2 source code for helloworldsessionhome.java import java.rmi.*; /** defines the methods for creating a helloworldsession */ public interface helloworldsessionhome extends ejbhome /** creates a helloworldsession bean with default settings */ /** creates a helloworldsession bean with a specific initial greeting */ } |
接口是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 import java.rmi.*; /** the implementation class for the helloworldsession bean */ public class helloworldsessionimpl implements sessionbean /** the session context provided by the ejb container. a session bean must 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) /** called by the ejb container when a client calls the create() method in public void ejbcreate() /** called by the ejb container when a client calls the public void ejbcreate(string agreeting) /** called by the ejb container to wake this session bean up after it public void ejbactivate() /** called by the ejb container to tell this session bean that it is being public void ejbpassivate() /** called by the ejb container to tell this session bean that it has been public void ejbremove() /** returns the session's greeting */ public string getgreeting() /** changes the session's greeting */ public void setgreeting(string 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。
闽公网安备 35060202000074号