EJB - JNDI 绑定
JNDI 代表 Java 命名和目录接口。 它是一组API和服务接口。 基于 Java 的应用程序使用 JNDI 进行命名和目录服务。 在 EJB 上下文中,有两个术语。
Binding(绑定) − 这是指为EJB对象分配一个名称,以便稍后使用。
Lookup(查找) − 这是指查找并获取EJB的对象。
在Jboss中,会话bean默认按照以下格式绑定在JNDI中。
local − EJB-name/local
remote − EJB-name/remote
如果 EJB 与
local − application-name/ejb-name/local
remote − application-name/ejb-name/remote
默认绑定示例
请参阅EJB - 创建应用程序章节的 JBoss 控制台输出。
JBoss应用服务器日志输出
... 16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3 16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean 16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibrarySessionBean/remote - EJB3.x Default Remote Business Interface LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x Remote Business Interface ...
自定义绑定
以下注解可用于自定义默认的 JNDI 绑定 −
local − org.jboss.ejb3.LocalBinding
remote − org.jboss.ejb3.RemoteBindings
更新LibrarySessionBean.java。 请参阅EJB - 创建应用程序章节。
LibrarySessionBean
package com.tutorialspoint.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
@Stateless
@LocalBinding(jndiBinding="tutorialsPoint/librarySession")
public class LibrarySessionBean implements LibrarySessionBeanLocal {
List<String> bookShelf;
public LibrarySessionBean() {
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
LibrarySessionBeanLocal
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Local;
@Local
public interface LibrarySessionBeanLocal {
void addBook(String bookName);
List getBooks();
}
构建项目,在 Jboss 上部署应用程序,并在 Jboss 控制台中验证以下输出 −
... 16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3 16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibrarySessionBean ejbName: LibrarySessionBean 16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: tutorialsPoint/librarySession - EJB3.x Default Local Business Interface tutorialsPoint/librarySession-com.tutorialspoint.stateless.LibrarySessionBeanLocal - EJB3.x Local Business Interface ...

