Welcome 微信登录

首页 / 软件开发 / JAVA / EJB 2.0有状态SessionBean

EJB 2.0有状态SessionBean2011-09-14 51cto博客 leizhimin环境:

JBoss 4.0.2

JDK1.5

IDEA8.1.4

一、EJB开发

package lavasoft.testejb20yzt;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
* Bean类
* 所有具体的业务逻辑都在此类里面,此类不抛出远程异类
*
* @author leizhimin 2010-3-31 15:26:53
*/
public class HelloStatefulBean implements SessionBean /
private SessionContext context;
private String someOne;
/**
* 业务方法
*
* @return Hello的消息
*/
public String sayHello() /
System.out.println("HelloStatefulBean: sayHello() is called!");
return "Hello, " + someOne + "!";
}
/**
* 必须有这个方法,这是EJB的一条原则,这个方法不是来自SessionBean接口的
* 这只能是一个有状态的bean,因为无状态的bean除了无参数的create方法外, 不能有其他的创建方法
*/
public void ejbCreate(String someOne) /
System.out.println("HelloStatefulBean: ejbCreate(String someOne) is called!");
this.someOne = someOne;
}
//-------下面四个方法是来自SessionBean的,必须写出来,但没啥用-------
public void ejbActivate() /
System.out.println("HelloStatefulBean: ejbActivate() is called!");
}
public void ejbPassivate() /
System.out.println("HelloStatefulBean: ejbPassivate() is called!");
}
public void ejbRemove() /
System.out.println("HelloStatefulBean: ejbRemove() is called!");
}
public void setSessionContext(SessionContext sessionContext) /
System.out.println("HelloStatefulBean: setSessionContext() is called!");
context = sessionContext;
}
}

package lavasoft.testejb20yzt;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
/**
* 组件接口
* 所有的业务方法都要在此接口中声明
*
* @author leizhimin 2010-3-31 15:31:10
*/
public interface HelloStateful extends EJBObject /
/**
* 业务方法,组件接口中的业务方法必须抛出RemoteException
*
* @return
* @throws java.rmi.RemoteException
*/
public String sayHello() throws RemoteException;
}