企业JavaBeansTM学习指南:
建立你的第一个无状态(stateless)会话Bean
- 关于本学习指南
- 关于示例
- 步骤1: 安装企业JavaBeans 服务器
- 步骤2: 指定企业JavaBeans 远程界面
- 步骤3: 指定宿主界面
- 步骤4: 编写企业JavaBean 类
- 步骤5: 创建ejb-jar 文件
- 步骤6: 配置DemoBean企业JavaBeans
- 步骤7: 编写企业JavaBean 客户程序
- 步骤8: 运行客户程序
3. 如何建立你的第一个无状态(stateless)会话Bean
步骤 1: 安装企业 JavaBeans服务器
添加 weblogic/classes 和 weblogic/lib/weblogicaux.jar到Java CLASSPATH上。确认 "." 或当前目录也在CLASSPATH上。 在LD_LIBRARY_PATH 上添加weblogic/lib/solaris http://www.sun.com/solaris/java 5)编辑 BEA WebLogic 的属性文件 weblogic.properties 设置系统口令, weblogic.password.system
在建立并配置示例 bean 之后, 要检查企业 JavaBeans 的 bean 是否已被正确配置, 可通过服务器命令行窗口来检查,或通过打开控制台以检查在"分布式对象"下的"Enterprise JavaBeans Transaction Server",你将看到 DemoBean 已被配置,也可以监测它的活动。
步骤2: 指定企业JavaBeansTM 远程界面
- 请阅读下面的解释;
- 将代码样本存储到指定的文件里。
/**
* Demo -- this is the "remote" interface of
* our enterprise JavaBean, it
* defines only one simple method called
* demoSelect(). As this is meant to be
* the simplest of examples demoSelect()
* never goes to a database, it just
* returns a string
*
* Note: The implementation of this interface is
* provided by the container tools
* but the demoSelect() method and any
* other methods in this interface
* will need to have equivalent
* implementations in the demobean.java
* which is supplied by the bean writer
* ..i.e., you!
*/
package ejb.demo;
import java.rmi.RemoteException;
import java.rmi.Remote;
import javax.ejb.*;
public interface Demo extends EJBObject, Remote {
// NB this simple example does not even do a
// lookup in the database
public String demoSelect() throws RemoteException;
}
步骤3: 指定宿主界面 (Home Interface)
/**
* DemoHome.java - This is the Home interface it must
* extend javax.ejb.EJBHome and define one or more
* create() methods for the bean.
*
* Note: The implementation of this interface is
* generated by the container tools.
*/
package ejb.demo;
import javax.ejb.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.*;
/**
* This interface is extremely simple it declares only
* one create method.
*/
public interface DemoHome extends EJBHome {
public Demo create() throws CreateException,
RemoteException;
}
步骤4: 编写企业JavaBeanTM 类
/**
* DemoBean -- This is implemented by the EnterPrise
* Bean author This class must extend
* javax.ejb.SessionBean and implement
* the methods in this interface as well as providing
* the implementation of the business methods.
*
*/
package ejb.demo;
import javax.ejb.*;
import java.io.Serializable;
import java.util.*;
import java.rmi.*;
public class DemoBean implements SessionBean {
static final boolean verbose = true;
private transient SessionContext ctx;
private transient Properties props;
// Implement the methods in the SessionBean
// interface
public void ejbActivate() {
if (verbose)
System.out.println("ejbActivate called");
}
public void ejbRemove() {
if (verbose)
System.out.println("ejbRemove called");
}
public void ejbPassivate() {
if (verbose)
System.out.println("ejbPassivate called");
}
/**
* Sets the session context.
*
* @param SessionContext
*/
public void setSessionContext(SessionContext ctx) {
if (verbose)
System.out.println("setSessionContext called");
this.ctx = ctx;
props = ctx.getEnvironment();
}
/**
* This method corresponds to the create method in
* the home interface DemoHome.java.
* The parameter sets of the two methods are
* identical. When the client calls
* DemoHome.create(), the container allocates an
* instance of the EJBean and calls ejbCreate().
*/
public void ejbCreate () {
if (verbose)
System.out.println("ejbCreate called");
}
/**
* **** HERE IS THE BUSINESS LOGIC *****
* Do the demoSelect() but don't even go to
* the database in this eg but instead just
* return a String.
* The really BIG thing to notice here is that
* this is the only code we have invented at all
* the rest of the code has been declarations
* or simply implementing methods which are
* part of the EJB interfaces and in this example
* are not even used.
*/
public String demoSelect()
throws RemoteException
{
return("hello world");
}
}
建立你的第一个无状态会话Bean--HelloWorld之一
建立你的第一个无状态会话Bean--HelloWorld之二
建立你的第一个无状态会话Bean--HelloWorld之三
230



被折叠的 条评论
为什么被折叠?



