如何在WebSphere Liberty中使用JNDI查找EJB [英] How to lookup EJB using JNDI in WebSphere Liberty

查看:688
本文介绍了如何在WebSphere Liberty中使用JNDI查找EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Liberty Application Server执行JNDI名称搜索。但是,它正在扔我一个依赖注入问题。附加是我的代码

I am trying to perform a JNDI name search using Liberty Application Server.However, It is throwing me a dependency injection issue.Attached is my code

我正在得到以下错误:

[ERROR   ] SRVE0319E: For the [com.airline.controllers.FlightDetails]   
 servlet, com.airline.controllers.FlightDetails servlet class was  
 found, but a resource injection failure has occurred. CWNEN0030E: The   
 server was unable to obtain an object instance for the  
 java:comp/env/com.airline.controllers.FlightDetails/flightService  
 reference.  

 The exception message was: CWNEN1003E: The server was unable to find   
 the Web1/FlightService binding with the    
 com.airline.service.FlightService type for the   
 java:comp/env/com.airline.controllers.FlightDetails/flightService   
 reference.

FlightService.java

FlightService.java

package com.airline.service;
import java.io.Serializable;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
@Stateless
@LocalBean
public class FlightService implements Serializable {

public FlightService() {
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getFrom() {
    return from;
}

public void setFrom(String from) {
    this.from = from;
}
public String getTo() {
    return to;
}
public void setTo(String to) {
    this.to = to;
}
public Integer getPrice() {
    return price;
}
public void setPrice(Integer price) {
    this.price = price;
}
public Integer getNumOfSeats() {
    return numOfSeats;
}
public void setNumOfSeats(Integer numOfSeats) {
    this.numOfSeats = numOfSeats;
}
public String getAirplaneModel() {
    return airplaneModel;
}
public void setAirplaneModel(String airplaneModel) {
    this.airplaneModel = airplaneModel;
}
private Integer id =23467;
private String from="Los Angles";
private String to="London";
private Integer price=400;
private Integer numOfSeats=250;
private String airplaneModel="Boeing 787";

}

----- - ****** FlightDetails.java ******* -------------

------******FlightDetails.java*******-------------

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
  PrintWriter out = response.getWriter();
  response.setContentType("text/html");
  try {
      flightService = (FlightService)new InitialContext().lookup("java:comp/env/Web1/FlightService");
      System.out.println(flightService);

  }
  catch(Exception ex){
      ex.printStackTrace();
  }
  if(flightService !=null){
      out.println(flightService.getAirplaneModel());
      out.println(flightService.getFrom());
      out.println(flightService.getTo());

  }

}

----- ******** server.xml ********* ----------------

--------********server.xml*********----------------

<jndiObjectFactory id="flightService"                         
 className="com.airline.service.FlightService"/>
 <jndiReferenceEntry jndiName="Web1/FlightService"   
  factoryRef="flightService"/> 
 <webApplication id="Web1" location="Web1-0.0.1-SNAPSHOT.war"   
  name="Web1"/> 
 <!-- <application context-root="Web1" type="war" id="Web1"
location="Web1-0.0.1-SNAPSHOT.war" name="Web1"/>-->
</server>

任何帮助将不胜感激!

推荐答案

< jndiObjectFactory> 元素允许您提供javax.naming.spi.ObjectFactory的自定义实现,它将创建和返回正在查找对象的一个​​实例。但是,似乎您正在尝试查找EJB,EJB引用将由ejb功能提供,而不是应用程序代码。

The <jndiObjectFactory> element allows you to provide a custom implementation of javax.naming.spi.ObjectFactory, which will create and return an instance of the object being looked up. However, it appears you are attempting to lookup an EJB, and EJB references would be provided by the ejb feature and not application code.

最简单的方法是查找 EJB是在messages.log中查找如下消息:

The easiest way to "lookup" an EJB is to look in messages.log for a message like this:

CNTR0167I:服务器将FlightService企业bean的com.airline.service.FlightService接口绑定到的Web1.war模块的????应用。绑定位置是:java:global / ???? / Web1 / FlightService!com.airline.service.FlightService

CNTR0167I: The server is binding the com.airline.service.FlightService interface of the FlightService enterprise bean in the Web1.war module of the ???? application. The binding location is: java:global/????/Web1/FlightService!com.airline.service.FlightService

然后,将查找字符串更改为值的绑定位置。这是EJB在JNDI中绑定的位置,应该用什么来直接查找。

Then, change your lookup string to be the value of the binding location. This is where the EJB has been bound in JNDI, and what should be used to directly look it up.

EJB也可以在java中使用:app和java:module,这将类似于java:全局查找字符串,但省略了应用程序或模块名称。例如,java:app / Web1 / FlightService或者java:module / FlightService。
当然,EJB必须在同一应用程序中的java:app或相同的模块用于java:module。注意,如果EJB只实现一个接口,那么!< interface> 可能会被关闭。

EJBs are also available in java:app and java:module, which would be similar to the java:global lookup string, but leaving out the application or module name. For example, java:app/Web1/FlightService or java:module/FlightService. Of course, the EJB must be in the same application for java:app or same module for java:module. Note, the !<interface> may be left off if the EJB only implements a single interface.

EJB不能直接在java中使用:comp。然而,您可以声明将在java中绑定的EJB引用:comp / env。例如,在web.xml中这样的东西

EJBs are not available directly in java:comp. You may however declare an EJB reference that will be bound in java:comp/env. For example, something like this in web.xml

  <ejb-local-ref>
    <ejb-ref-name>Web1/FlightService</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>com.airline.service.FlightService</local>
    <ejb-link>FlightService</ejb-link>
  </ejb-local-ref>

< ejb-ref-name> 然后绑定到java:comp / env,所以应用程序可能会查找java:comp / env / Web1 / FlightService。只要EJB位于同一应用程序中,< ejb-link> 元素将引用到名为FlightService的真实EJB的映射。

The <ejb-ref-name> is then bound into java:comp/env, so the application may lookup java:comp/env/Web1/FlightService. The <ejb-link> element maps that reference to the real EJB named FlightService, as long as the EJB is located in the same application.

如果你不在乎使用< ejb-link> ,或者你想覆盖它,可以通过添加一个条目在ibm-web-bnd.xml中类似于这样:

If you do not care to use <ejb-link>, or you wish to override it, that may be done by adding an entry in ibm-web-bnd.xml similar to this:

<ejb-ref name="Web1/FlightService" binding-name="java:global/????/Web1/FlightService!com.airline.service.FlightService"/>

绑定文件中的此条目将参考java:comp / env / Web1 / FlightService映射到真实的EJB java:global / ???? / Web1 / FlightService!com.airline.service.FlightService。

This entry in the binding file maps the reference java:comp/env/Web1/FlightService to the real EJB java:global/????/Web1/FlightService!com.airline.service.FlightService.

这篇关于如何在WebSphere Liberty中使用JNDI查找EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆