|
楼主 |
发表于 2006-7-17 23:15
|
显示全部楼层
一 建立EJB项目.记住选择J2EE1.3规范(具体原因myeclipse帮助已经说明).
二 按CTRL+N -> MYECLIPSE -> EJB ->SESSION BEAN...由于本实例是要创建SLSB,,,所以使用默认选项即可,,,读者只需选择一个你喜欢的包,还有起一个你喜欢的名字.我这里创建在org.woden.ejb.HelloEJB...好了,接着点击finish.
三 完成第二步后,我们已经创建了一个实现SessionBean接口的bean,并且工具帮你在bean内完成了必要的方法...十分方便.接着我们开始在bean内写业务方法....HelloEJB代码如下:
package com.woden.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
* XDoclet-based session bean. The class must be declared public according to
* the EJB specification.
*
* To generate the EJB related files to this EJB: - Add Standard EJB module to
* XDoclet project properties - Customize XDoclet configuration for your
* appserver - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="Hello" display-name="Name for Hello" description="Description
* for Hello" jndi-name="ejb/Hello" type="Stateless"
* view-type="remote"
*/
public class HelloEJB implements SessionBean {
/** The session context */
private SessionContext context;
public HelloEJB() {
// TODO Auto-generated constructor stub
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
/**
* Set the associated session context. The container calls this method after
* the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException
* Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "remote"
*
* @throws EJBException
* Thrown if method fails due to system-level error.
*/
public void replaceWithRealBusinessMethod() throws EJBException {
// rename and start putting your business logic here
}
// 业务方法
/**
* @ejb.interface-method
*/
public String sayHello(String name) {
return "Hello!" + name;
}
}
这里需要特别注意,业务方法的注释@ejb.interface-method是必须的,它通知XDoclet能够生成相应的接口.(PS..至于XDoclet是什么,本版有另一位同学写了篇用XDoclet生成HIBERNATE映射文件的文章,大家可以参考下)...这里为了简单起见,省略其他注释...
前部分的注释是JNDI名,这里在客户端程序会用到,在后文笔者会提到.关于JNDI也是J2EE里面一个很重要概念,如果不很了解,建议参看相关书籍.
[ 本帖最后由 wool王 于 2006-7-18 00:30 编辑 ] |
|