NutzCN Logo
问答 ioc支持生成可变参数的对象吗?
发布于 3049天前 作者 qq_041ad963 1754 次浏览 复制 上一个帖子 下一个帖子
标签:

因为想在配置文件处控制程序,所以想设计这种参数是可变的,testClass_1/2/3是单例。
代码如下,请问可否生成TestClassArray 对象?
我试了下 生成失败了。或许有什么方法支持生成这种可变参数对象的吗?
js:
testOutList: {
type: 'com.TestClassArray',
args : [{refer: 'testClass_1'}, {refer: 'testClass_2'}, {refer: 'testClass_3'}]
}

java:
public class TestClassArray {
public TestClassArray(Object... args){
...
}
}

7 回复

应该可以的, 抛什么错误,贴完整.

"种类1/2/3"是传入的参数Object值
log如下:
17:29:10.237 [main] ERROR [main] MainTest 78 main - 通过Ioc装载业务逻辑对象失败:[testOutList] # FAIL to create Ioc Bean name=[testOutList]
org.nutz.ioc.IocException: [testOutList] # FAIL to create Ioc Bean name=[testOutList]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265) ~[nutz-1.r.57.jar:?]
at com.MainTest.main(MainTest.java:70) [main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0]
at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:?]
Caused by: org.nutz.lang.born.BorningException: Fail to born 'com.TestClassArray'
by args: [
@(种类1)
@(种类2)
@(种类3)]
at org.nutz.lang.Mirror.getBorning(Mirror.java:954) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:107) ~[nutz-1.r.57.jar:?]
... 8 more

Process finished with exit code 0

试试这样写

args :[[{},{},{}]]

来自炫酷的 NutzCN

请问 我改写成args :[[{refer: 'testClass_1'}, {refer: 'testClass_2'}, {refer: 'testClass_3'}]]
也是失败了

09:20:39.437 [main] ERROR [main] MainTest 78 main - 通过Ioc装载业务逻辑对象失败:[testOutList] # FAIL to create Ioc Bean name=[testOutList]
org.nutz.ioc.IocException: [testOutList] # FAIL to create Ioc Bean name=[testOutList]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:265) ~[nutz-1.r.57.jar:?]
at com.MainTest.main(MainTest.java:70) [main/:?]
Caused by: org.nutz.lang.born.BorningException: Fail to born 'com.TestClassArray'
by args: [
@([种类1, 种类2, 种类3])] becasue:
java.lang.IllegalArgumentException: wrong number of arguments
at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:23) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114) ~[nutz-1.r.57.jar:?]
... 3 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[?:1.7.0]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.7.0]
at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[?:1.7.0]
at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67) ~[nutz-1.r.57.jar:?]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114) ~[nutz-1.r.57.jar:?]
... 3 more
Disconnected from the target VM, address: '127.0.0.1:50305', transport: 'socket'

Process finished with exit code 0

已经没问题了 就是按照您说的这么改的 多谢赐教~~

我刚刚试了一下

// test.json
{
	abc : {type:"java.lang.Object"},
	def : {type:"java.lang.Object"},
	ghi : {type:"java.lang.Object"},
	testClassArray: {
        type: 'net.wendal.nutzbook.TestClassArray',
 		args : [{refer: 'abc'}, {refer: 'def'}, {refer: 'ghi'}]
    }
}

TestClassArray类与你的一样

package net.wendal.nutzbook;

public class TestClassArray {
    public TestClassArray(Object... args) {}
}

testcase方法

    @Test
    public void test_ioc_var_args() throws IOException {
        ioc.get(TestClassArray.class);
    }

是可以pass的哦.

要不你把testClass_1等定义也贴出来吧

testClass_1跟您写的例子也一样 就是返回个文字列。
刚才是我手误 ,您提供的方法没问题。谢谢了。

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