Spring rmi配置

[size=large][color=red][b]Spring Rmi配置[/b][/color][/size]
[b][size=large]1、服务器端[/size][/b]
新建一个Dynamic 、Web Project,导入支持Spring jar包。在WEB-INF下新建applicationContent.xml(必须加上<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">,否则会报错),代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="baseRmiService" class="com.wym.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.wym.service.BaseService"></property>
<!-- 注册端口号 -->
<property name="registryPort" value="1200"></property>
</bean>
</beans>


BaseService.java
package com.wym.service;

import com.wym.bi.User;

public interface BaseService {

public String getHelloword(String name);

public String getUser(User user);
}


BaseServiceImpl.java
package com.wym.service.impl;

import com.wym.bi.User;
import com.wym.service.BaseService;

public class BaseServiceImpl implements BaseService {

public String getHelloword(String name){
return "Welcome to Shanghai," + name + "!";
}

public String getUser(User user){
return "name: " + user.getName() + "------>" + "age: " + user.getAge();
}
}


User:必须实现Serializable接口
package com.wym.bi;

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 com.wym.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wym.service.BaseService;

public class BaseServiceTest {

/**
* @param args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BaseService baseService = (BaseService) context.getBean("baseRmiService");
System.out.println("baseRmiService start...");
}

}

[size=medium]把service借口和java bean打成jar包放到客户端的里面。
启动main方法,或者将service发布到服务器。[/size]

[size=large][b]2、客户端[/b][/size]
新建一个java项目或者web项目,导入Spring jar包。新建applicationContext.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- baseService是调用服务端serviceName的value -->
<property name="serviceUrl" value="rmi://192.168.0.37:1200/baseService"></property>
<!-- service接口 -->
<property name="serviceInterface" value="com.wym.service.BaseService"></property>
</bean>
</beans>

192.168.0.37是服务器的IP地址。

main方法
package wym.client;

import com.wym.bi.User;
import com.wym.service.BaseService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientTest {

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:d:/tnt_project/TEST/conf/applicationContext.xml");
BaseService baseService = (BaseService) context.getBean("baseService");
System.out.println(baseService.getHelloword("Yunmin Wu"));
User user = new User();
user.setName("Dan Qiao");
user.setAge(48);
System.out.println(baseService.getUser(user));
}
}


关于Spring的applicationContent.xml文件的详解,请点此链接:[url]http://ithead.iteye.com/admin/blogs/1461565[/url]。
运行main方法,在控制台就能看见结果了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:以下是一个简单的 Spring RPC 示例: 1.创建一个 Maven 项目并添加以下依赖项: ``` <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> </dependencies> ``` 2.创建一个接口: ``` public interface CalculatorService { int add(int a, int b); } ``` 3.创建一个实现类: ``` public class CalculatorServiceImpl implements CalculatorService { @Override public int add(int a, int b) { return a + b; } } ``` 4.在 Spring 配置文件中定义 Bean: ``` <bean id="calculatorService" class="com.example.CalculatorServiceImpl" /> ``` 5.创建一个 Spring Controller: ``` @Controller public class CalculatorController { @Autowired private CalculatorService calculatorService; @RequestMapping(value="/add", method=RequestMethod.POST) @ResponseBody public int add(@RequestParam("a") int a, @RequestParam("b") int b) { return calculatorService.add(a, b); } } ``` 6.在 Spring 配置文件中注册 Controller: ``` <mvc:annotation-driven /> <context:component-scan base-package="com.example" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> ``` 7.对外提供 RPC 服务,使用 RMI 或 Hessian 等协议。 ``` <bean id="calculatorServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="serviceName" value="CalculatorService" /> <property name="service" ref="calculatorService" /> <property name="serviceInterface" value="com.example.CalculatorService" /> <property name="registryPort" value="1099" /> </bean> <!-- 或者 --> <bean id="calculatorServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="calculatorService" /> <property name="serviceInterface" value="com.example.CalculatorService" /> </bean> ``` 8.在客户端通过 RMI 或 Hessian 等协议访问 CalculatorService。 ``` // RMI Remote remoteService = Naming.lookup("rmi://localhost:1099/CalculatorService"); CalculatorService calculatorService = (CalculatorService) UnicastRemoteObject.exportObject(remoteService, 0); // Hessian String url = "http://localhost:8080/CalculatorService"; HessianProxyFactory factory = new HessianProxyFactory(); CalculatorService calculatorService = (CalculatorService) factory.create(CalculatorService.class, url); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值