首先我先说明一下EJB开发的步骤:1)编写Home接口(用create()方法产生远程接口的实例),Remote接口(用于声明业务逻辑方法)和EJB(用于实现Reomte接口中声明的业务逻辑方法)2)编译以上文件3)编写部署描述文件(ejb-jar.xml和weblogic-ejb-jar.xml)4)将已经编译的class文件和部署文件打包5)将以上jar包编译为ejb所用的jar包6)部署,测试下面我将按以上步骤以一个简单的HelloWorld的例子来说明-------------------------------------------------------------------------------------------------------
Home接口代码:其必须继承EJBHome,并且声明返回Remote接口的create()方法package testEJB;
import java.rmi.*;
import javax.ejb.*;
public interface HelloWorldHome extends EJBHome{
public HelloWorld create()throws RemoteException,CreateException;
}
-------------------------------------------------------------------------------------------------------
Remote接口代码:其必须继承EJBObject,并且声明业务逻辑方法,如sayHello()
package testEJB;
import java.rmi.*;
import javax.ejb.*;
public interface HelloWorld extends EJBObject{
public String sayHello()throws RemoteException;
}
--------------------------------------------------------------------------------------------------------
EJB类代码:其继承SessionBean,并且实现了业务逻辑方法和SessionBean接口中的所有方法
package testEJB;
import java.rmi.*;
import javax.ejb.*;
public class HelloWorldBean implements SessionBean{
private SessionContext sct;
private String words;
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
}
public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
// TODO Auto-generated method stub
this.sct=arg0;
}
public void ejbCreate(){
words="Hello!~!~~!!~,nanqiangma";
}
public String sayHello(){
String replay="this is a ejb example: "+words;
return replay;
}
}
--------------------------------------------------------------------------------------------------------------
接下来按照步骤编译,接着写部署描述文件,描述文件如下:
ejb-jar.xml文件,声明和EJB组件有关的信息
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<!-- Generated XML! -->
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>StatelessSessionBean</ejb-name>
<home>TradeHome</home>
<remote>Trade</remote>
<ejb-class>TradeBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<env-entry>
<env-entry-name>limit</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>6000</env-entry-value>
</env-entry>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>StatelessSessionBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
----------------------------------------------------------------------------------------------------------
weblogic-ejb-jar文件,声明和运行环境有关的信息
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>
<!-- Generated XML! -->
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>StatelessSessionBean</ejb-name>
<stateless-session-descriptor>
<pool>
</pool>
<stateless-clustering>
</stateless-clustering>
</stateless-session-descriptor>
<transaction-descriptor>
</transaction-descriptor>
<enable-call-by-reference>True</enable-call-by-reference>
<jndi-name>StatelessSessionBean</jndi-name>
<remote-client-timeout>0</remote-client-timeout>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
---------------------------------------------------------------------------------------------------------------
接下来将class文件和部署文件打包(命令:jar -cvf testEJB.jar *)
接着编译已经打好的jar包:
java weblogic-ejbc -compiler javac testEJB.jar test_EJB.jar 将testEJB.jar编译为EJB容器能识别的test—_EJB.jar
最后部署(直接拷贝到weblogic 的Application目录下,或者在Console上部署),测试
测试代码:
package testEJB;
import java.rmi.RemoteException;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
public class TestEJB{
public HelloWorld init(){
HelloWorld hw=null;
try {
Properties pt=new Properties();
pt.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
pt.put(Context.PROVIDER_URL,"t3://localhost:7001");
Context ctx=new InitialContext(pt);
Object obj=ctx.lookup("testEJB");
HelloWorldHome hwh=(HelloWorldHome)PortableRemoteObject.narrow(obj,HelloWorldHome.class);
hw=hwh.create();
}
catch (Exception ex) {
ex.printStackTrace();
}
return hw;
}
public static void main(String args[]){
TestEJB t=new TestEJB();
try {
String replay=t.init().sayHello();
System.out.println(replay);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}