1、新建J2EE项目A、B,在A、B项目中建立com.A.xfire包
2、项目A、B中导入spring-2.5.6.jar
3、在A、B项目中使用建立接口类com.A.xfire.XfireFacade.java
package com.A.xfire;
/**
* XfireFacade接口类
*/
public interface XfireFacade {
public void xfireMothod();
}
4、在A项目中建立com.A.xfire.impl.XfireFacadeImpl实现接口类
package com.A.xfire.impl;
import com.A.xfire.XfireFacade;
public class XfireFacadeImpl extends JdbcDaoSupportimplements XfireFacade {
public void xfireMothod(){
/** 操作数据库 */
}
}
5、项目A中新建applicationContext-xfire.xml
6、在项目A中新建的applicationContext-xfire.xml文件中添加以下内容:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
7、在B项目中使用建立客户端连接,新建com.A.xfire.BXfireClient.java
package com.A.xfire;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import com.A.xfire.XfireFacade;
public class BXfireClient {
private static XfireFacade xfireFacade;
private static String bServiceUrl = "rmi://192.168.1.1:1199";
public static String getBServiceUrl() {
return bServiceUrl;
}
public static void setBServiceUrl(String bServiceUrl) {
BXfireClient.bServiceUrl = bServiceUrl;
}
public static XfireFacade findBXfireFacade() {
if(xfireFacade == null) {
xfireFacade = registerXfireFacade();
}
return xfireFacade;
}
/**
* 查找xfire服务
*/
public static XfireFacade registerXfireFacade() {
RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
rmiProxyFactoryBean.setServiceInterface(XfireFacade.class);
rmiProxyFactoryBean.setServiceUrl(getBServiceUrl());
rmiProxyFactoryBean.setLookupStubOnStartup(false);
rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true);
try {
rmiProxyFactoryBean.afterPropertiesSet();
} catch (RuntimeException e) {
e.printStackTrace();
}
if(rmiProxyFactoryBean.getObject() instanceof XfireFacade) {
xfireFacade = (XfireFacade) rmiProxyFactoryBean.getObject();
return xfireFacade;
}
return null;
}
}
8、注意如果接口有实体,实体类一定要implements Serializable
private static final long serialVersionUID = 1L;
9、在B项目其它类里调用RMI如下:
BXfireClient.findBXfireFacade().xfireMothod();