NutzCN Logo
精华 Nutz ioc 如何注入数组或可变参数
发布于 177天前 作者 GX 178 次浏览 复制 上一个帖子 下一个帖子
标签: ioc

Nutz ioc 如何注入数组或可变参数

工厂方法

public static Server createTcpServer(String... args) throws SQLException {
    TcpServer service = new TcpServer();
    Server server = new Server(service, args);
    service.setShutdownHandler(server);
    return server;
}

ioc配置

h2Server : {
    type : "org.h2.tools.Server",
    args : [
        [ "-tcp", "-tcpAllowOthers", "-tcpPort", "8082" ]
    ],
    factory : "org.h2.tools.Server#createTcpServer",
    events : {
        create : "start",
        depose : "stop"
    }
}

异常

Exception in thread "main" org.nutz.ioc.IocException: [h2Server] # FAIL to create Ioc Bean name=[h2Server]
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.gx.shiro.Main.main(Main.java:32)
	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.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.NoSuchMethodException: Fail to find Method org.h2.tools.Server->createTcpServer with params:
["java.util.ArrayList"]
	at org.nutz.lang.Mirror.findMethod(Mirror.java:1142)
	at org.nutz.lang.Mirror.findMethod(Mirror.java:1118)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:101)
	... 8 more

5 回复

我看看啥原因

我报个issue, 如果你急用的话,可以这样写

public class H2Helper {
     public static Server createTcpServer(List<String> args) {
	      return org.h2.tools.Server.createTcpServer(args.toArray(new String[0]));
	 }
}

经测试, 目前仍然存在问题

Exception in thread "main" org.nutz.ioc.IocException: [h2Server] # FAIL to create Ioc Bean name=[h2Server]
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at test.Main.main(Main.java:14)
	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.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.nutz.lang.born.BorningException: Fail to born 'org.h2.tools.Server'
 by args: [
  @([-tcp, -tcpAllowOthers, -tcpPort, 8082])] becasue:
java.lang.IllegalArgumentException: argument type mismatch
	at org.nutz.lang.born.MethodBorning.born(MethodBorning.java:20)
	at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
	... 8 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	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 org.nutz.lang.born.MethodBorning.born(MethodBorning.java:17)
	... 10 more

当程序执行到born方法中时, args中存储的值为ArrayList类型, 而createTcpServer的参数为数组, 所以无法进行调用
另外, 在Nutz ioc的json配置中似乎无法将数组参数注入, 只能注入列表

添加回复
请先登陆
回到顶部