[size=large]Spring Rmi配置[/size]
[color=darkred]服务端:[/color]
新建一个web项目, 并添加Spring能力.
applicationContext.xml:
IBaseService:
BaseServiceImpl:
User:
必须实现Serializable接口.
main:
把service接口和java bean打包为jar, 并添加到客户端里面.
启动main方法. 或者部署到tomcat里并启动.
[color=darkred]客户端:[/color]
新建一个java项目或web项目, 并添加spring能力.
applicationContext.xml:
Test:
现在运行Test类就会在控制台打印出:
欢迎hao的到来!!!
名字:chenghao--->年龄:21
[color=darkred]服务端:[/color]
新建一个web项目, 并添加Spring能力.
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="baseRmiService" class="com.my.service.impl.BaseServiceImpl"></bean>
<bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 调用Service -->
<property name="service" ref="baseRmiService"></property>
<!-- value值是提供给客户端调用 -->
<property name="serviceName" value="baseService"></property>
<!-- service接口 -->
<property name="serviceInterface" value="com.my.service.IBaseService"></property>
<!-- 注册端口 -->
<property name="registryPort" value="1200"></property>
</bean>
</beans>
IBaseService:
package com.my.service;
import com.my.po.User;
public interface IBaseService {
/**
* 简单Hello Word
* @param name
* @return
*/
public String getHelloWord(String name);
/**
* 获得User对象
* @param user
* @return
*/
public String getUser(User user);
}
BaseServiceImpl:
package com.my.service.impl;
import com.my.po.User;
import com.my.service.IBaseService;
public class BaseServiceImpl implements IBaseService {
public String getHelloWord(String name) {
return "欢迎"+name+"的到来!!!";
}
public String getUser(User user) {
return "名字:"+user.getName()+"--->"+"年龄:"+user.getAge();
}
}
User:
必须实现Serializable接口.
package com.my.po;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public User(){}
public User(String name, int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
main:
package test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.my.service.IBaseService;
public class BaseServiceTest {
/**
* @param args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IBaseService ibs = (IBaseService) context.getBean("baseRmiService");
System.out.println("baseRmiService启动...");
}
}
把service接口和java bean打包为jar, 并添加到客户端里面.
启动main方法. 或者部署到tomcat里并启动.
[color=darkred]客户端:[/color]
新建一个java项目或web项目, 并添加spring能力.
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- baseService是调用服务端serviceName的value -->
<property name="serviceUrl" value="rmi://localhost:1200/baseService"></property>
<!-- service接口 -->
<property name="serviceInterface" value="com.my.service.IBaseService"></property>
</bean>
</beans>
Test:
package com.my.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.my.po.User;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IBaseService ibs = (IBaseService) context.getBean("baseService");
System.out.println(ibs.getHelloWord("hao"));
User user = new User();
user.setName("chenghao");
user.setAge(21);
System.out.println(ibs.getUser(user));
}
}
现在运行Test类就会在控制台打印出:
欢迎hao的到来!!!
名字:chenghao--->年龄:21