NutzCN Logo
问答 小白可得,小黑丢失啊
发布于 2429天前 作者 ftqiao 1582 次浏览 复制 上一个帖子 下一个帖子
标签:

学习ioc,根据文档demo,没看出来哪里错写错了啊。
js:

var ioc = {
    xiaobai : {
        name : 'XiaoBai',
        birthday : '2009-10-25 15:23:40'
    },
    xiaohei : {
        type : 'org.nutz.demo.Pet', // 类型
        singleton : false, // 是否为单件
        args : [ 'XiaoHei' ], // 构造函数参数
        fields : {
            birthday : '2009-11-3 08:02:14',
            friend : {refer : 'xiaobai'}    // 指向容器里另外一个对象
        }
    }
}
    @Test
    public void test_get_xiaohei() {
        Ioc ioc = new NutIoc(new JsonLoader("pet.js"));
        Pet pet = ioc.get(null, "xiaohei");
        System.out.println(pet.getBirthday().getTimeZone().getID());
        System.out.println(pet.getName());
        ioc.depose();
    }

ioc.get()方法指定类型或者null都报错。nutz使用1.r.65-SNAPSHOT版本
日志如下:

java.lang.RuntimeException: IocBean[xiaohei] fail at field=[friend]

at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
at org.nutz.ioc.ObjectProxy.get(ObjectProxy.java:69)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:260)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
at org.nutz.demo.PetTest.test_get_xiaohei(PetTest.java:24)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.nutz.ioc.IocException: IocBean[xiaobai] NULL TYPE object 'xiaobai'
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:213)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 27 more

5 回复

先获取小白,使其类型确定,然后再获取小黑

@Test
    public void test_get_xiaohei() {
        Ioc ioc = new NutIoc(new JsonLoader("pet.js"));
        Pet xiaobai = ioc.get(Pet.class,"xiaobai");
        Pet pet = ioc.get(null, "xiaohei");
        System.out.println(pet.getBirthday().getTimeZone().getID());
        System.out.println(pet.getName());
        ioc.depose();
    }

如大神指点,无错误。
根据下面这段文档理解的话,感觉容易误解。

如果配置文件中声明了类型,则可不传入类型
Pet xh = ioc.get(null, "xiaohei");
System.out.printf("%s's friend is %s\n", xh.getName(), xh.getFriend().getName());

感谢秒回。回复信息在浏览器这边提示出来,是什么技术?

html5的notify api

多谢多谢~

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