Vertx上实现IOC的问题

来源:互联网 发布:网络电台软件下载 编辑:程序博客网 时间:2024/06/03 13:23

问题由来:

想在Vertx中实现IOC容器,已定义@Inject,@Component注解

//第二种方式************************************************************************************public static void secondIOC () throws Exception {    Class<?> controllerClass = Class.forName("com.andaren.interfaces.testInject.HandlerControllerTest");    Object controllerObj = searchInjectClass(controllerClass);    controllerClass.getDeclaredMethods()[0].invoke(controllerObj);}/** * IOC尝试: *  第二个版本: *      遍历每个需要注入的field,生成对应域的对象 * @param clazz * @return * @throws Exception */public static Object searchInjectClass (Class<?> clazz) throws Exception {    // analysis field    Field[] fields = clazz.getDeclaredFields();    // generate the field instance    Object clazzInstance = clazz.newInstance();    // go through all fields    for (Field f : fields) {        Inject injectedField = f.getAnnotation(Inject.class);        if (injectedField != null) {            Class<?> fieldType = f.getType();            Object fieldObj = searchInjectClass(fieldType);            f.setAccessible(true);            f.set(clazzInstance, fieldObj);            continue;        } else {            continue;        }    }    return clazzInstance;}//*********************************************************************************************//第一种方式************************************************************************************/** * IOC简单尝试 *  第一个版本: *      private 反射可以跨过域访问控制权限给域赋值 * * @throws Exception */public static void simpleFindMethod () throws Exception {    Class<?> getServiceClass = Class.forName("com.andaren.interfaces.testInject.GetService");    Object getServiceObject = getServiceClass.newInstance();    Field[] fields = getServiceClass.getDeclaredFields();    for (Field f : fields) {        Inject inject = f.getAnnotation(Inject.class);        if (inject != null) {            System.out.println("Find the inject field");            AService service = new AService();            f.setAccessible(true);            f.set(getServiceObject, service);        }    }    Method m = getServiceClass.getDeclaredMethods()[0];    m.invoke(getServiceObject, null);    System.out.println("over");}//*************************************************************************************************
代码如上:

将第二种方法,在main中循环一百次,

代码如下:

        Long startTime = System.currentTimeMillis();        for (int i = 0; i < 100 ; i++) {//            AService aService = new AService();//            GetService getService = new GetService();//            getService.setaService(aService);//            HandlerControllerTest test = new HandlerControllerTest();//            test.setGetService(getService);//            test.testInject();            secondIOC();        }        Long endTime = System.currentTimeMillis();        System.out.println("Cost Time " + (endTime - startTime));        System.exit(0);

run main方法第三次的时候抛出异常:

Connected to the target VM, address: '127.0.0.1:64127', transport: 'socket'
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.andaren.interfaces.handler.DispatherHandler.secondIOC(DispatherHandler.java:413)
at com.andaren.interfaces.handler.DispatherHandler.main(DispatherHandler.java:402)
Disconnected from the target VM, address: '127.0.0.1:64127', transport: 'socket'


Process finished with exit code 1

很奇怪,前两次都能统计到时间在20ms之内,第三次总是报参数异常...

为什么呢?

待解决.

有时报这个异常

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]

越来越奇怪

原创粉丝点击