阅读(3755) 赞(4)

EJB JNDI绑定

2016-12-07 10:42:16 更新

JNDI表示Java命名和目录接口。这是一组API和服务接口。基于Java应用程序使用JNDI命名和目录服务。在EJB的情况下,有两个方面。

  • Binding 结合 -这是指将一个名称分配给一个 ejb 对象,可以在以后使用

  • Lookup 查询 -这是指查找和获取 ejb 对象。


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

  • local 本地 -ejb-name /local

  • remote 远程 -ejb-name /remote


在下面的情况下,EJB与<application-name>/<应用程序名称>捆绑. ejb.ear 文默认格式如下。

  • local 本地 -应用程序的名称/ EJB名/本地 application-name/ejb-name/local

  • remote 远程 -应用程序的名称/ EJB名/远程 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
...

重复以上步骤,用于远程和检查结果。