2017-02-14 49 views
1

我試圖執行使用自由應用Server.However一個JNDI名稱搜索,它扔我一個依賴注入issue.Attached是我的代碼如何查找使用JNDI EJB在WebSphere自由

我正在以下錯誤:

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

任何幫助,將不勝感激!

回答

4

<jndiObjectFactory>元素允許您提供javax.naming.spi.ObjectFactory的自定義實現,它將創建並返回正在查找的對象的實例。但是,看起來您正在嘗試查找EJB,並且EJB引用將由ejb功能提供,而不是應用程序代碼。

最簡單的方法「查找」的EJB是messages.log尋找這樣的消息:

CNTR0167I:服務器綁定FlightService企業bean的com.airline.service.FlightService接口在Web1.war模塊中?應用。綁定位置是:java:global/????/Web1/FlightService!com.airline.service.FlightService

然後,將您的查找字符串更改爲綁定位置的值。這是JNDI中綁定EJB的地方,應該用什麼來直接查找它。

EJB也可用於java:app和java:模塊,它與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位於相同的應用程序中,就可以引用名爲FlightService的真實EJB的<ejb-link>元素映射。

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

該條目:

如果你不小心使用<ejb-link>,或者您希望覆蓋它,可以通過添加IBM的web-bnd.xml類似下面的條目進行綁定文件將引用java:comp/env/Web1/FlightService映射到真正的EJB java:global/????/Web1/FlightService!com.airline.service.FlightService。

-1

既然你正試圖從Servlet訪問無狀態EJB,最簡單的將是剛剛在servlet類注入EJB爲:

@EJB 
private FlightService flightService; 

,而不是使用initialcontext.lookup。那麼你根本不必關心JNDI的名字。

+0

我知道我們可以做到這一點,我正在學習JNDI。 –