-
最近有個需求,要求通過配置文件中配置的內容,來執行不同的操作,要比較容易利於以後擴展,就想到利用發射類實現這個需求,簡單的寫了個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消息]
}
}