在Jboss 中要自定义JNDI 名称,可以使用@LocalBinding 和@RemoteBinding 注释,@LocalBinding 注释指定Session Bean 的Local 接口的JNDI 名称,@RemoteBinding 注释指定Session Bean 的Remote 接口的JNDI 名称,变Session Bean 的JNDI 名称
package com.foshanshop.ejb3.impl; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import com.foshanshop.ejb3.LocalOperation; import com.foshanshop.ejb3.Operation; import org.jboss.annotation.ejb.LocalBinding; import org.jboss.annotation.ejb.RemoteBinding; @Stateless @Remote ({Operation.class}) @RemoteBinding (jndiBinding="foshanshop/RemoteOperation") @Local ({LocalOperation.class}) @LocalBinding (jndiBinding="foshanshop/LocalOperation") public class OperationBean implements Operation, LocalOperation { private int total = 0; private int addresult = 0; public int Add(int a, int b) { addresult = a + b; return addresult; } public int getResult() { total += addresult; return total; } }
在JSP 客户端调用上面EJB 的代码片断如下:
InitialContext ctx = new InitialContext(props);
Operation operation = (Operation) ctx.lookup("foshanshop/RemoteOperation");
InitialContext ctx = new InitialContext(props);
Operation operation = (Operation) ctx.lookup("foshanshop/RemoteOperation");
如果你使用的是weblogic10/Sun Application Server/Glassfish,你可以使用@Stateless.mappedName()设置bean 的全局JNDI 名称,如:
@Stateless(mappedName="OperationBeanRemote") public class OperationBean implements Operation, LocalOperation {客户端访问EJB 的代码片断如下:
InitialContext ctx = new InitialContext(props); Operation operation = (Operation) ctx.lookup("OperationBeanRemote#com.foshanshop.ejb3.Operation");