主要包括如下类

RMIBeansManager.java

RMIDemo.java

RMIService.java

RMIVO.java

SayHello.java

RMIVO.java

package com.sssppp.RMI;

public class RMIVO {
private String beanName; //对应的bean
private String method; //方法名
private String params; //方法参数 public String getBeanName() {
return beanName;
} public void setBeanName(String beanName) {
this.beanName = beanName;
} public String getMethod() {
return method;
} public void setMethod(String method) {
this.method = method;
} public String getParams() {
return params;
} public void setParams(String params) {
this.params = params;
} }

RMIService.java

package com.sssppp.RMI;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import com.sssppp.Json.jackson.Utils.JsonUtil; public class RMIService {
public String call(RMIVO rmi) {
Object bean = RMIBeansManager.getBean(rmi.getBeanName());
Method method = findMethod(bean, rmi.getMethod()); String retValue = null;
if (method == null) {
System.out.println("Can not get method: " + rmi.getMethod());
} else {
try {
Object obj;
if (rmi.getParams() == null) {
obj = method.invoke(bean);
} else {
obj = method.invoke(bean, rmi.getParams());
}
retValue = convertToJson(obj);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return retValue;
} private Method findMethod(Object bean, String methodName) {
for (Method method : bean.getClass().getMethods()) {
if (method.getName().equals(methodName)) {
return method;
}
}
return null;
} private String convertToJson(Object obj) {
try {
return JsonUtil.parseObjectToJsonString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return null; } }

RMIBeansManager.java

用于获取bean

package com.sssppp.RMI;

import javax.ws.rs.POST;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; @Component
public class RMIBeansManager { private static ApplicationContext context = null; @POST
private static void init() {
try {
context = new ClassPathXmlApplicationContext(
"/conf/spring-conf/rmiBean.xml");
} catch (Exception e) { }
} public static Object getBean(String beanName) {
Object retObject = null;
retObject = getContext().getBean(beanName);
return retObject;
} public static ApplicationContext getContext() {
if (context == null) {
init();
}
return context;
}
}

对应的rmiBean.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <context:component-scan base-package="com.sssppp.RMI" /> </beans>

SayHello.java

package com.sssppp.RMI;

import org.springframework.stereotype.Component;

@Component("sayHello")
public class SayHello {
public void welcome(String name) {
System.out.println("Welcome , hello " + name);
}
}

RMIDemo.java

package com.sssppp.RMI;

public class RMIDemo {

	public static void main(String[] args) {
RMIVO rmivo = new RMIVO();
rmivo.setBeanName("sayHello");
rmivo.setMethod("welcome");
rmivo.setParams("Tom"); RMIService rmiService = new RMIService();
rmiService.call(rmivo);
}
}

调试结果

Welcome , hello Tom
05-11 22:56