-
最近有个需求,要求通过配置文件中配置的内容,来执行不同的操作,要比较容易利于以后扩展,就想到利用发射类实现这个需求,简单的写了个demo类,测试了一下,可以使用,就记录了下来,有空拿到项目总修改一下就可以使用了
-
配置文件send.properties,通过配置文件配置的key选择要实例化的类
1000=SendTextMessage
2000=SendJsonMessage
- 发送消息接口
package com.momoda.statistics.test;
import java.util.List;
/**
* @author xuyangyang
*/
public interface SeneMessage {
public List send(List param);
}
- 实现SeneMessage,发送文本消息
package com.momoda.statistics.test;
import java.util.ArrayList;
import java.util.List;
/**
* @author xuyangyang
*/
public class SendTextMessage implements SeneMessage {
@Override
public List send(List param) {
List<String> result = new ArrayList<>();
result.add(new String("向客户发送普通文本消息"));
result.add(new String("/n"));
result.addAll(param);
return result;
}
}
- 实现SeneMessage,发送json消息
package com.momoda.statistics.test;
import java.util.ArrayList;
import java.util.List;
/**
* @author xuyangyang
*/
public class SendJsonMessage implements SeneMessage {
@Override
public List send(List param) {
List<String> result = new ArrayList<>();
result.add(new String("向客户发送json消息"));
result.add(new String("/n"));
result.addAll(param);
return result;
}
}
- 通过反射调用
package com.momoda.statistics.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 测试通过不同的配置文件发送消息
*
* @author xuyangyang
*/
public class TestReflectSendMessage {
/**
* 加载配置文件
*
* @param sendCommand
* @return
*/
public String laodProperties(String sendCommand) {
String result = null;
FileInputStream fis = null;
try {
Properties prop = new Properties();
fis = new FileInputStream("D:\\devproject\\momoda-statistics\\src\\main\\java\\com\\momoda\\statistics\\test\\send.properties");
prop.load(fis);
result = prop.getProperty(sendCommand);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* @param sendCommand 发送命令,通过配置文件配置要调用的类,发送消息
* @param paramContent 发送内容,
* @return
* @throws InvocationTargetException 为了逻辑清楚,异常全部抛出,
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
*/
public String sendMessage(String sendCommand, String paramContent) throws InvocationTargetException, IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException {
String result = null;
String className = "com.momoda.statistics.test." + this.laodProperties(sendCommand);
//加载类
Class<?> c = Class.forName(className);
//创建类的实例
SeneMessage object = (SeneMessage) c.newInstance();
// 构造参数列表类型
Class params[] = new Class[1];
params[0] = Class.forName("java.util.List");
//查询act方法
Method method = c.getMethod("send", params);
//构造方法参数
Object args[] = new Object[1];
List array = new ArrayList<String>();
array.add(paramContent);
args[0] = array;
//调用方法并返回
Object returnObjcet = method.invoke(object, args);
System.out.println(returnObjcet);
result = returnObjcet.toString();
return result;
}
/**
* 测试发送消息
*/
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
TestReflectSendMessage tr = new TestReflectSendMessage();
tr.sendMessage("1000", "文本消息");
// [向客户发送普通文本消息, /n, 文本消息]
tr.sendMessage("2000", "json消息");
//[向客户发送json消息, /n, json消息]
}
}