Ejb 简明教程

EJB - JNDI Bindings

JNDI 代表 Java 命名和目录接口。它是一组 API 和服务接口。基于 Java 的应用程序将 JNDI 用于命名和目录服务。在 EJB 的背景下,有两个术语。

  1. Binding − 指为 EJB 对象分配一个名称,稍后可以使用该名称。

  2. Lookup − 指查找并获取 EJB 对象。

在 Jboss 中,默认情况下,会话 bean 以以下格式绑定在 JNDI 中。

  1. local − EJB-name/local

  2. remote − EJB-name/remote

如果 EJB 与 <application-name>.ear 文件捆绑在一起,则默认格式如下 −

  1. local − application-name/ejb-name/local

  2. remote − application-name/ejb-name/remote

Example of Default Binding

请参阅 EJB - 创建应用程序章节的 JBoss 控制台输出。

JBoss Application Server Log Output

...
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
...

Customized Binding

可以使用以下注释来自定义默认 JNDI 绑定 −

  1. local − org.jboss.ejb3.LocalBinding

  2. 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
...