Spring rmi配置
Spring Rmi配置
1、服务器端
新建一个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" 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>
package com.wym.service;import com.wym.bi.User;public interface BaseService {public String getHelloword(String name);public String getUser(User user);}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();}}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; } }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...");}}<?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" value="rmi://192.168.0.37:1200/baseService"></property><!-- service接口 --><property name="serviceInterface" value="com.wym.service.BaseService"></property></bean></beans>
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)); }}