1.添加相应的jar包到Ejb项目中,编下以下几个类:package zl.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote({IHelloWorld.class})
public class HelloWorld implements IHelloWorld {
public String sayHello() {
return "这是我第一个EJB项目";
}
}
2.将Ejb项目打包导出,
3.登录到weblogic控制台,部署->安装,
4.安装完毕之后在JNDI树中进行查看:
下面是一个测试类:
package test;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import zl.ejb.HelloWorld;
import zl.ejb.IHelloWorld;
public class testEjb {
public static void main(String[] args) {
System.out.println("begin ctx");
Hashtable cs = new Hashtable();
cs.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
cs.put(Context.PROVIDER_URL, "t3://localhost:7001");
try
{
Context ctx = new InitialContext(cs);
IHelloWorld objRef = (IHelloWorld)ctx.lookup("java:global.HelloEjb.HelloWorld");
System.out.println(objRef.getClass());
System.out.println(objRef.sayHello());
System.out.println("end ctx");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
测试结果如下: