spring rmi配置

[size=large]Spring Rmi配置[/size]

[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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值