EJBJNDI綁定

JNDI代表Java命名和目錄介面。它是一組API和服務介面。基於Java的應用程式使用JNDI命名和目錄服務。在EJB的背景下,有兩個方面。

  • Binding - 這指的是以後可以使用一個EJB對象分配一個名稱。

  • Lookup - 這指的是尋找並獲得EJB對象。

在JBoss中,會話bean綁定到JNDI,默認情況下有以下格式。

  • local - ejb-name/local

  • remote - ejb-name/remote

情況下,EJB捆綁在一起<application-name> ear檔默認格式如下。

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

重複上述步驟,為遠程和檢查結果。

 


上一篇: EJB安全 下一篇: EJB實體關係