如何在WebSphere Liberty中使用JNDI查找EJB

时间:2017-02-14 01:25:06

标签: java-ee ejb jndi websphere-liberty

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

我收到以下错误:

[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

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

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

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

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

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

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

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

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

EJB也可以在java:app和java:module中使用,它类似于java:global查找字符串,但是省略了应用程序或模块名称。例如,java:app / Web1 / FlightService或java:module / FlightService。 当然,EJB必须位于java:app的相同应用程序中,或者位于java:module的相同模块中。注意,如果EJB仅实现单个接口,则!<interface>可能会被取消。

EJB不能直接在java:comp中使用。但是,您可以声明将在java:comp / env中绑定的EJB引用。例如,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。

如果您不想使用<ejb-link>,或者您希望覆盖它,可以通过在ibm-web-bnd.xml中添加类似于此的条目来完成:

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

答案 1 :(得分:-1)

由于您尝试从servlet访问无状态EJB,最简单的方法就是将Servlet类中的EJB注入:

df = pd.DataFrame({ 'id_var1' : [1] * 8 + [2] * 7
                    ,'id_var2' : [i for i in range(1,9)] + [i for i in range(1,8)]
                   ,'num_var1' : [1,1,2,2,3,3,3,4] + [1,2,5,2,1,1,2]
               ,'num_var2' : [1, 0,0,3,3,3,0,0] + [0,1,0,0,2,2,0]
              })

而不是使用@EJB private FlightService flightService; 。那么你根本不需要关心JNDI的名字。