springmvc的配置文件中加入
<bean name="/userInterface" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="userInterfaceImpl" />
<property name="serviceInterface" value="com.sj.login.service.UserInterface" />
</bean>
其中name="/userInterface" 为映射的地址
<property name="service" ref="userInterfaceImpl" />为服务类的实现类
<property name="serviceInterface" value="com.sj.login.service.UserInterface" /> 为服务类实现的接口
服务类的实现类
@Service
public class UserInterfaceImpl implements UserInterface{
@Resource
private UserManagerService userManagerService;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public int insertSelective(String name,String password){
String newPwd=MD5Util.getMD5(password, "utf-8");
UserManager user=new UserManager();
user.setPassword(newPwd);
user.setUsername(name);
//return 1;
return userManagerService.insertSelective(user);
}
}
客户端调用方法这个客户端项目部署在了本机的另一个tomcat下端口号为8082
public class Test {
interface SumSay {//名字随便起只要保证方法名和服务端的方法名一致就行
int insertSelective(String name,String password);
}
public static void main(String[] args) throws MalformedURLException {
//TODO 根据实际地址修改
String url = "http://localhost:8080/login/userInterface";
HessianProxyFactory factory = new HessianProxyFactory();
SumSay sumSay = (SumSay) factory.create(SumSay.class, url);
System.out.println(sumSay.insertSelective("ceshi","123456"));
}
}
其中我遇到的无法注入的原因是被拦截器拦截了,修改一下拦截器就行
参考原文:地址:http://www.cnblogs.com/xdp-gacl/p/3897534.html
原文是把采用将服务端的IService和User两个类打包成jar包提供给客户端进行调用,保证两个一摸一样的类