spring_webservice_rmi配置示例

来源:互联网 发布:淘宝vip福利群 编辑:程序博客网 时间:2024/05/17 22:34

服务器端: 
图片

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
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/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 ;
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop ;
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx ;
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ;
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache ;
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ;
">
  
 <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> 
 
  
     
</beans> 


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-->

发布==》

调用服务器
图片

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="http://www.springframework.org/schema/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 ;
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop ;
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx ;
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ;
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache ;
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ;
">
   
 
 <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();
}
 
 }
     
}

 运行测试ok

原创粉丝点击