反射(四):利用反射实现类的动态加载

  1. 最近有个需求,要求通过配置文件中配置的内容,来执行不同的操作,要比较容易利于以后扩展,就想到利用发射类实现这个需求,简单的写了个demo类,测试了一下,可以使用,就记录了下来,有空拿到项目总修改一下就可以使用了

  2. 配置文件send.properties,通过配置文件配置的key选择要实例化的类




1000=SendTextMessage

2000=SendJsonMessage


  1. 发送消息接口
package com.momoda.statistics.test;

import java.util.List;

/**
 * @author xuyangyang
 */
public interface SeneMessage {

    public List send(List param);

}

  1. 实现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;
    }
}

  1. 实现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;
    }
}

  1. 通过反射调用
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消息]
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值