通过简单代码,深度剖析IOC/DI 及java Reflection之间的关系

2 篇文章 0 订阅
1 篇文章 0 订阅
package com.myibs.test.service.reflection;

import lombok.Data;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@Data
public class ReflectionDemo {
    String className;
    Class class0;
    Integer classParamCnt;
    Class<?>[] classParam;
    Constructor contructor;

    Integer instanceParamCnt;
    Object[] InstanceParam;
    Object objInstance;

    Integer methodParamCnt;
    Class<?>[] methodParam;
    String methodName;
    Class[] methodParamTypes;
    Object[] methodArgs;

    Method method;

    Object returnObject;

    public void setClass0(String className) throws ClassNotFoundException {
        this.className = className;
        class0 = Class.forName(className);
    }

    public void setClassParam(ArrayList<Class> classParamLst) {
        classParam = classParamLst.toArray(new Class[classParamLst.size()]);
        instanceParamCnt = this.classParam.length;
    }

    public void setConstructor() throws NoSuchMethodException {
        contructor = class0.getConstructor(classParam);
    }

    public void setInstanceParam(ArrayList<Object> instanceParamLst) {
        InstanceParam = instanceParamLst.toArray(new Object[instanceParamLst.size()]);
        instanceParamCnt = InstanceParam.length;
    }

    public void setObjInstance() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        objInstance = contructor.newInstance(InstanceParam);
    }

    public void setMethodParam(ArrayList<Class> methodParamLst) {
        methodParam = methodParamLst.toArray(new Class[methodParamLst.size()]);
        methodParamCnt = methodParam.length;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public void setMethodParamTypes(ArrayList<Class> methodParamTypesList) {
        methodParamTypes = methodParamTypesList.toArray(new Class[methodParamTypesList.size()]);
    }

    public void setMethodArgs(ArrayList<Object> methodArgsList) {

        methodArgs = methodArgsList.toArray(new Object[methodArgsList.size()]);
    }

    public void setMethod() throws NoSuchMethodException {
        method = class0.getMethod(methodName,methodParamTypes);
    }

    public Object callMethod() throws InvocationTargetException, IllegalAccessException {
        returnObject = method.invoke(objInstance, methodArgs);
        return returnObject;
    }

    public static void main(String[] args) {
        /****************************************************************
         * 1. 以下代码可以使用XML/JSON方式读取, 具体可参照spring Bean Factory
         ****************************************************************/
        String className;
        ArrayList<Class> classParamLst = new ArrayList();
        ArrayList<Object> instanceParamLst = new ArrayList();
        String methodName;
        ArrayList<Class> methodParamLst = new ArrayList();
        ArrayList<Class> methodParamTypesList = new ArrayList();
        ArrayList<Object> setMethodArgsList = new ArrayList();

        className = "com.myibs.test.service.reflection.Person";

        classParamLst.add(String.class);
        classParamLst.add(int.class);
        classParamLst.add(String.class);

        instanceParamLst.add("小明111");
        instanceParamLst.add(222);
        instanceParamLst.add("哈尔滨111");

        methodName = "method4";

        methodParamLst.add(String.class);
        methodParamTypesList.add(String.class);
        setMethodArgsList.add("itcast");

        /****************************************************************
         * 2. 此段代码可使用建造者方式配置进 本类
         ****************************************************************/

        /****************************************************************
         * 3. 装配代码正式开始
         ****************************************************************/
        try {
            ReflectionDemo reflectionDemo = new ReflectionDemo();
            reflectionDemo.setClass0(className);
            reflectionDemo.setClassParam(classParamLst);
            reflectionDemo.setConstructor();
            reflectionDemo.setInstanceParam(instanceParamLst);
            reflectionDemo.setObjInstance();
            reflectionDemo.setMethodName(methodName);
            reflectionDemo.setMethodParam(methodParamLst);
            reflectionDemo.setMethodParamTypes(methodParamTypesList);
            reflectionDemo.setMethodArgs(setMethodArgsList);
            reflectionDemo.setMethod();
            reflectionDemo.callMethod();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}

@Data
class Person implements Serializable {
    //成员变量
    public String name;
    public int age;
    private String address;

    private final static long serialVersionUID= -123123612836L;

    //构造方法
    public Person() {
        System.out.println("空参数构造方法");
    }

    public Person(String name) {
        this.name = name;
        System.out.println("带有String的构造方法");
    }
    //私有的构造方法
    private Person(String name, int age){
        this.name = name;
        this.age = age;
        System.out.println("带有String,int的构造方法");
    }

    public Person(String name, int age, String address){
        this.name = name;
        this.age = age;
        this.address = address;
        System.out.println("带有String, int, String的构造方法");
    }

    //成员方法
    //没有返回值没有参数的方法
    public void method1(){

        System.out.println("没有返回值没有参数的方法");
    }
    //没有返回值,有参数的方法
    public void method2(String name){

        System.out.println("没有返回值,有参数的方法 name= "+ name);
    }
    //有返回值,没有参数
    public int method3(){
        System.out.println("有返回值,没有参数的方法");
        return 123;
    }
    //有返回值,有参数的方法
    public String method4(String name){
        System.out.println("有返回值,有参数的方法");
        return "哈哈" + name;
    }
    //私有方法
    private void method5(){
        System.out.println("私有方法");
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", address=" + address+ "]";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值