服务器端:
![图片](https://i-blog.csdnimg.cn/blog_migrate/527a5ce30c2d09794c1ccca3d3895a22.png)
Userinfo.java
package com.lilin.entity;
IUserinfoService.java
UserinfoServiceImpl.java
web.xml
![图片](https://i-blog.csdnimg.cn/blog_migrate/527a5ce30c2d09794c1ccca3d3895a22.png)
Userinfo.java
package com.lilin.entity;
import java.io.Serializable;
public class Userinfo implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
IUserinfoService.java
package com.lilin.iservice;
import com.lilin.entity.Userinfo;
public interface IUserinfoService {
public void addUserinfo(Userinfo userinfo) throws Exception;
}
UserinfoServiceImpl.java
package com.lilin.serviceimpl;
import com.lilin.entity.Userinfo;
import com.lilin.iservice.IUserinfoService;
public class UserinfoServiceImpl implements IUserinfoService{
public void addUserinfo(Userinfo userinfo) throws Exception{
System.out.println("添加了一个新的用户"+userinfo.getName());
}
}
applicationContext.xml
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans ;
">
<bean id="userinfoService" class="com.lilin.serviceimpl.UserinfoServiceImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- does not necessarily have to be the same name as the bean to be exported -->
<property name="serviceName" value="UserinfoService"/>
<property name="service" ref="userinfoService"/>
<property name="serviceInterface" value="com.lilin.iservice.IUserinfoService"/>
<!-- defaults to 1099 -->
<property name="registryPort" value="1199"/>
</bean>
web.xml
<!-- load spring begin-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- load spring end-->
发布==》
调用服务器
![图片](https://i-blog.csdnimg.cn/blog_migrate/814b84f2102935b92e8f83406cb3cf2e.png)
userinfo.java 和 IUserinfoService.java同上
SimpleObject.java
App.java
运行测试ok
发布==》
调用服务器
![图片](https://i-blog.csdnimg.cn/blog_migrate/814b84f2102935b92e8f83406cb3cf2e.png)
userinfo.java 和 IUserinfoService.java同上
SimpleObject.java
package com.lilin.test;
import com.lilin.entity.Userinfo;
import com.lilin.iservice.IUserinfoService;
public class SimpleObject {
private IUserinfoService iUserinfoService;
public IUserinfoService getiUserinfoService() {
return iUserinfoService;
}
public void setiUserinfoService(IUserinfoService iUserinfoService) {
this.iUserinfoService = iUserinfoService;
}
public void add(Userinfo name) throws Exception{
iUserinfoService.addUserinfo( name );
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans ;
">
<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1199/UserinfoService"/>
<property name="serviceInterface" value="com.lilin.iservice.IUserinfoService"/>
</bean>
<bean id="test" class="com.lilin.test.SimpleObject">
<property name="iUserinfoService" ref="accountService"/>
</bean>
</beans>
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lilin.entity.Userinfo;
import com.lilin.test.SimpleObject;
public class App {
@org.junit.Test
public void test(){
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
SimpleObject object = (SimpleObject) context.getBean("test");
try {
Userinfo userinfo = new Userinfo();
userinfo.setName("=>李林");
object.add(userinfo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}