EJB中的那三个类
那三个类
--- 此处以EJB中的SessionBean为例
1. 远程接口 --extends EJBObject
实例:
public interface PoAssetService extends javax.ejb.EJBObject {
public VoPoAsset getPoAssetById(AppContext appContext,long assetId) throws RemoteException, PoAssetException, AppSystemException;
-----
}
2. 本地接口 --extends EJBHome
功能:提供远程接口对象的创建。
实例:
public interface PoAssetServiceHome extends javax.ejb.EJBHome {
public com.pti.fsc.poi.services.poasset.PoAssetService create()
throws javax.ejb.CreateException, java.rmi.RemoteException;
}
3. Bean实现类--implements javax.ejb.SessionBean
实例:
即:PoAssetServiceBean,PoAssetService的实现类(面向接口编程哦)
总结一下:本地接口继承EJBHome,作用是产生EJB对象的引用,进而引用EJB实例。EJB继承自EJBObject,
其中定义了所有的业务方法,放到了实现类中去实现,EJB实现类继承自SesstionBean
这样,我们要调用PoAssetServiceBean(实际调用PoAssetServic就可以,为什么呢,面向接口编程嘛~~),就可以利用Home类PoAssetServiceHome 来生成 对PoAssetService的引用,代码如下:
public static final String SERVICE_NAME = "ejb/com/XXX/fsc/poi/services/poasset/PoAssetServiceHome";
private static PoAssetServiceHome serviceHome = null;
private static Logger logger = Logger.getInstance(PoAssetServiceAccessor.class.getName());
private PoAssetService service = null;
static {
try {
serviceHome =
(PoAssetServiceHome)ResourceLocator.getInstance().getRemoteHome(SERVICE_NAME, PoAssetServiceHome.class);
} catch (Exception e) {
logger.error("Service " + SERVICE_NAME + " can not be located.", e);
}
}
public PoAssetServiceAccessor() {
try {
if (serviceHome != null)
service = serviceHome.create();
} catch (Exception e) {
logger.error("creating service error", e);
}
}
public VoPoAsset getPoAssetById(long assetId) throws RemoteException, PoAssetException, AppSystemException {
return (service != null ? service.getPoAssetById(AppContextUtil.createAppContext(), assetId) : null);
}