google-code-export / nutz

Automatically exported from code.google.com/p/nutz
Apache License 2.0
1 stars 1 forks source link

Nutz解析Ioc的js配置文件的几个Bug! #210

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
我用的是 Nutz1.a.27 版本,而且是Nutz文档里面ioc入门的例子。

第一个:相互引用的Bug
ioc配置文件pet.js
var ioc = {
    xiaohei : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'XiaoHei',
            birthday : '2010-05-31 00:40:50',
            friend : {
                refer : 'xiao3'
            }
        }
    },

    xiao3 : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'Xiao3',
            birthday : '1987-12-08 00:40:50',
            friend : {
                refer : 'xiaohei'
            }
        }
    }
}

客户端代码:
public class HelloPet {

    public static void main(String[] args) {
        Ioc ioc = new NutIoc(new JsonLoader("nutz/ioc/demo/pet.js"));
        Pet pet = ioc.get(null, "xiaohei");
        System.out.printf("%s - [%s]\n", pet.getName(),
pet.getBirthday().getTimeZone().getID());
        System.out.println(pet.getFriend().getName());
    }

}
直接抛 Exception in thread "main" java.lang.StackOverflowError 异常。

第二个:无法引用无类型定义的Bean
var ioc = {
    xiaobai : {
        name : 'XiaoBai',
        birthday : '2010-05-31 00:35:50'
    },

    xiaohei : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'XiaoHei',
            birthday : '2010-05-31 00:40:50',
            friend : {
                refer : 'xiabai'
            }
        }
    }
}
客户端代码同上,直接抛 Exception in thread "main"
org.nutz.ioc.IocException: For object [xiabai] - type:[null] 异常。
如果把friend定义改为
friend : {
    type : 'nutz.ioc.demo.Pet',
    refer : 'xiabai'
}
则打印:
XiaoHei - [Asia/Shanghai]
null
显然,还是无法生成或是引用名为‘xiaobai’的Bean。

第三个:莫名其妙的type字段
var ioc = {
    xiaohei : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'XiaoHei',
            birthday : '2010-05-31 00:40:50',
            friend : {
                refer : 'xiao3'
            }
        }
    },

    xiao3 : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'Xiao3',
            birthday : '1987-12-08 00:40:50',
            friend : {
                type : 'nutz.ioc.demo.Pet',
                refer : 'xiaohei'
            }
        }
    }
}
客户端代码同上,运行正常,打印:
XiaoHei - [Asia/Shanghai]
Xiao3
不过,如果在xiaohei的friend定义加上type,即:
xiaohei : {
    type : 'nutz.ioc.demo.Pet',
    fields : {
        name : 'XiaoHei',
        birthday : '2010-05-31 00:40:50',
        friend : {
            type : 'nutz.ioc.demo.Pet',
            refer : 'xiao3'
        }
    }
},
就会出错,打印:
XiaoHei - [Asia/Shanghai]
null
显然,还是引用名为‘xiao3’的Bean。

如果是
var ioc = {
    xiaohei : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'XiaoHei',
            birthday : '2010-05-31 00:40:50',
            friend : {
                type : 'nutz.ioc.demo.Pet',
                refer : 'xiao3'
            }
        }
    },

    xiao3 : {
        type : 'nutz.ioc.demo.Pet',
        fields : {
            name : 'Xiao3',
            birthday : '1987-12-08 00:40:50',
            friend : {
                refer : 'xiaohei'
            }
        }
    }
}
还是出错,打印:
XiaoHei - [Asia/Shanghai]
null

Original issue reported on code.google.com by leu...@gmail.com on 1 Jun 2010 at 9:11

GoogleCodeExporter commented 9 years ago
第一个问题, 已经有相关的issue
第二个问题, 
这是必然的,因为xiaohei没有type信息,根本无法进行创建. 
不过,这个使我想到相
关的一些问题.
第三个问题, 字段的type不能这样写的,请参阅 
http://code.google.com/p/nutz/wiki/ioc_hello

Original comment by wendal1985@gmail.com on 1 Jun 2010 at 10:31

GoogleCodeExporter commented 9 years ago
第一个问题相关的issue: http://code.google.com/p/nutz/issues/detail?id=94

Original comment by wendal1985@gmail.com on 1 Jun 2010 at 10:32

GoogleCodeExporter commented 9 years ago
第二个问题: 应该写成:
friend : {
    refer : 'xiabai:nutz.ioc.demo.Pet'
}

Original comment by wendal1985@gmail.com on 1 Jun 2010 at 10:43

GoogleCodeExporter commented 9 years ago
如果用 {refer:'xxx'} 这里面不能再用 type 
了,否则,就不能表示一个引用。

Original comment by zozoh...@gmail.com on 1 Jun 2010 at 10:50

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
我试着运行了代码:

在目录: http://code.google.com/p/nutzdemo/source/browse/

POJO 代码 Pet.java:  
/trunk/demo/hello_ioc/src/nutz/demo/ioc/book/Pet.java

IOC 配置文件  pets.js
/trunk/demo/hello_ioc/src/nutz/demo/ioc/book/pets.js

Main 函数: 
/trunk/demo/hello_ioc/src/nutz/demo/ioc/book/HelloPet.java

运行结果:

2010-06-01 18:47:54,674 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaobai'<class 
nutz.demo.ioc.book.Pet>
2010-06-01 18:47:54,691 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Load 
definition
2010-06-01 18:47:54,743 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Make...
2010-06-01 18:47:54,749 [main] DEBUG org.nutz.ioc.impl.ScopeContext - Save 
object 
'xiaobai' to [app] 
XiaoBai - [Asia/Shanghai]
2010-06-01 18:47:55,628 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaohei'<null>
2010-06-01 18:47:55,629 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Load 
definition
2010-06-01 18:47:55,646 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Make...
2010-06-01 18:47:55,695 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaobai'<null>
XiaoHei's friend is XiaoBai
2010-06-01 18:47:55,696 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaohei'<null>
2010-06-01 18:47:55,696 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Load 
definition
2010-06-01 18:47:55,696 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Make...
2010-06-01 18:47:55,698 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaobai'<null>
2010-06-01 18:47:55,698 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaohei'<null>
2010-06-01 18:47:55,698 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Load 
definition
2010-06-01 18:47:55,699 [main] DEBUG org.nutz.ioc.impl.NutIoc -      >> Make...
2010-06-01 18:47:55,699 [main] DEBUG org.nutz.ioc.impl.NutIoc - Get 
'xiaobai'<null>
false

Original comment by zozoh...@gmail.com on 1 Jun 2010 at 11:04

GoogleCodeExporter commented 9 years ago
现在明白了,相互引用只有在单例情况下才又意义。

Original comment by leu...@gmail.com on 1 Jun 2010 at 12:17

GoogleCodeExporter commented 9 years ago
friend : {
    refer : 'xiabai:nutz.ioc.demo.Pet'
}

xiaobai后面的的类型nutz.ioc.demo.Pet,理想情况下应该可以根据��
�它信息推断出来的吧。呵
呵……我的要求高了,何况自己也没什么贡献,而且要求实��
�多了也会引入更多本可以避免的复杂度。

Original comment by leu...@gmail.com on 1 Jun 2010 at 12:24

GoogleCodeExporter commented 9 years ago
是可以推断出来的,我前面的回复已经说了,你可以仔细对��
�一下那三个文件

Original comment by zozoh...@gmail.com on 1 Jun 2010 at 12:31

GoogleCodeExporter commented 9 years ago

Original comment by wendal1985@gmail.com on 25 Jun 2010 at 3:07