如何让客户端识别EJB项目中包含的对象类?

时间:2014-04-18 11:23:04

标签: java java-ee ejb

我使用EJB和远程接口创建了项目,如下所示。我还创建了一个类来保存此项目中的对象(ShipData)。我将此bean部署到服务器。然后我在一个新的Dynamic项目中创建了一个带有main方法的类,如下所示,我在其中注入了我创建的bean。问题似乎是没有识别类ShipData。任何人都可以告诉我他们是否可以从我所做的事情中看到一些根本错误的东西。我也为服务器和客户端项目展示了我的文件结构。谢谢你的帮助。

EJB

@Stateless
public class ShipBean implements ShipBeanRemote {

private ShipData newShip;
private List<ShipData> listOfShips;

public void addShip(String shipName,int tonnage,String nationality){
    newShip=new ShipData(shipName, tonnage, nationality);
    listOfShips.add(newShip);

}

public ShipData searchShip(String shipName){
    for(int i=0; i<listOfShips.size(); i++){
        if(listOfShips.get(i).getShipName() != null && listOfShips.get(i).getShipName().equals(shipName));
           newShip=listOfShips.get(i);
    }
    return newShip;
}
}

远程接口

@Remote
public interface ShipBeanRemote {

public void addShip(String shipName,int tonnage,String nationality);


public ShipData searchShip(String shipName);

}

出货数据

public class ShipData{

private String shipName;
private int tonnage;
private String nationality;

public ShipData(String shipName, int tonnage, String nationality) {
    this.shipName = shipName;
    this.tonnage = tonnage;
    this.nationality = nationality;
}

public String getShipName() {
    return shipName;
}

public void setShipName(String shipName) {
    this.shipName = shipName;
}

public int getTonnage() {
    return tonnage;
}

public void setTonnage(int tonnage) {
    this.tonnage = tonnage;
}

public String getNationality() {
    return nationality;
}

public void setNationality(String nationality) {
    this.nationality = nationality;
}

}
使用main方法和EJB注入的新项目中的

public class SearchShips {

@EJB (lookup="java:global/ShippingBeans/ShipBean") 
 private ShipBeanRemote ship; 
 /* calculator is the representative of the bean, created 
 at runtime using dependency injection */ 


public static void main (String[] args){

    printAnabellaInfo();
}

public void printAnabellaInfo(){
ship.addShip("shipA",10,"Irish");
ship.addShip("Anabella",15,"French");
ship.addShip("shipC",12,"English");


ship.searchShip("Anabella");
System.out.println(ship.getShipName);

}
}

File Structure of both projects

1 个答案:

答案 0 :(得分:1)

ShipData是模型/数据bean /数据传输对象。将其移动到另一个项目(可以将其称为commonComponents或DataObjects)。现在从该项目中创建jar并将其放在客户端和服务器(EJB)项目的类路径中。

在ypur当前设置中,ShipData对象将仅暴露给服务器(EJB项目)。