use of org.nutz.ioc.Ioc in project nutz by nutzam.
the class SimpleJsonIocTest method test_java_with_arguments.
@Test
public void test_java_with_arguments() {
Ioc ioc = I(J("fox", "name:'Fox',age:10"), J("wolf", "name:{java:'$fox.showName(\"_\", 2, \"W\")'},age:{java:'$fox.age'}"));
Animal fox = ioc.get(Animal.class, "fox");
Animal wolf = ioc.get(Animal.class, "wolf");
assertEquals("Fox", fox.getName());
assertEquals(fox.getAge(), wolf.getAge());
assertEquals("__W", wolf.getName());
}
use of org.nutz.ioc.Ioc in project nutz by nutzam.
the class SimpleJsonIocTest method test_refer.
@Test
public void test_refer() {
Ioc ioc = I(J("fox", "type:'org.nutz.ioc.json.pojo.Animal',fields:{name:'Fox'}"), J("rabit", "name:'Rabit',enemies:[{refer:'fox'},{refer:'fox'}]"));
Animal r = ioc.get(Animal.class, "rabit");
Animal f = ioc.get(Animal.class, "fox");
assertEquals(2, r.getEnemies().length);
assertTrue(f == r.getEnemies()[0]);
assertTrue(f == r.getEnemies()[1]);
assertEquals("Fox", f.getName());
assertEquals("Rabit", r.getName());
}
use of org.nutz.ioc.Ioc in project nutz by nutzam.
the class SimpleJsonIocTest method test_singleon.
@Test
public void test_singleon() {
Ioc ioc = I(J("fox", "name:'Fox'"));
Animal f = ioc.get(Animal.class, "fox");
Animal f2 = ioc.get(Animal.class, "fox");
assertTrue(f == f2);
ioc = I(J("fox", "singleton:false, fields: {name:'Fox'}"));
Animal f3 = ioc.get(Animal.class, "fox");
Animal f4 = ioc.get(Animal.class, "fox");
assertFalse(f3 == f4);
}
use of org.nutz.ioc.Ioc in project nutz by nutzam.
the class XmlIocLoaderTest method test_simple_case.
@Test
public void test_simple_case() {
Ioc ioc = new NutIoc(getNew("org/nutz/ioc/loader/xml/conf/simple.xml"));
Bee c = ioc.get(Bee.class, "C");
assertEquals("TheC", c.getName());
assertEquals(15, c.getAge());
assertEquals("TheQueen", c.getMother().getName());
assertEquals(3, c.getFriends().size());
assertEquals("TheA", c.getFriends().get(0).getName());
assertEquals("TheB", c.getFriends().get(1).getName());
assertEquals(1, c.getMap().size());
assertEquals("ABC", c.getMap().get("abc"));
ioc.depose();
}
use of org.nutz.ioc.Ioc in project nutz by nutzam.
the class NutLoading method load.
public UrlMapping load(NutConfig config) {
if (log.isInfoEnabled()) {
log.infof("Nutz Version : %s ", Nutz.version());
log.infof("Nutz.Mvc[%s] is initializing ...", config.getAppName());
}
if (log.isDebugEnabled()) {
Properties sys = System.getProperties();
log.debug("Web Container Information:");
log.debugf(" - Default Charset : %s", Encoding.defaultEncoding());
log.debugf(" - Current . path : %s", new File(".").getAbsolutePath());
log.debugf(" - Java Version : %s", sys.get("java.version"));
log.debugf(" - File separator : %s", sys.get("file.separator"));
log.debugf(" - Timezone : %s", sys.get("user.timezone"));
log.debugf(" - OS : %s %s", sys.get("os.name"), sys.get("os.arch"));
log.debugf(" - ServerInfo : %s", config.getServletContext().getServerInfo());
log.debugf(" - Servlet API : %d.%d", config.getServletContext().getMajorVersion(), config.getServletContext().getMinorVersion());
if (config.getServletContext().getMajorVersion() > 2 || config.getServletContext().getMinorVersion() > 4)
log.debugf(" - ContextPath : %s", config.getServletContext().getContextPath());
log.debugf(" - context.tempdir : %s", config.getAttribute("javax.servlet.context.tempdir"));
log.debugf(" - MainModule : %s", config.getMainModule().getName());
}
/*
* 准备返回值
*/
UrlMapping mapping;
Ioc ioc = null;
/*
* 准备计时
*/
Stopwatch sw = Stopwatch.begin();
try {
/*
* 检查主模块,调用本函数前,已经确保过有声明 MainModule 了
*/
Class<?> mainModule = config.getMainModule();
/*
* 创建上下文
*/
createContext(config);
/*
* 检查 Ioc 容器并创建和保存它
*/
ioc = createIoc(config, mainModule);
/*
* 组装UrlMapping
*/
mapping = evalUrlMapping(config, mainModule, ioc);
/*
* 分析本地化字符串
*/
evalLocalization(config, mainModule);
// 初始化SessionProvider
createSessionProvider(config, mainModule);
/*
* 执行用户自定义 Setup
*/
evalSetup(config, mainModule);
} catch (Exception e) {
if (log.isErrorEnabled())
log.error("Error happend during start serivce!", e);
if (ioc != null) {
log.error("try to depose ioc");
try {
ioc.depose();
} catch (Throwable e2) {
log.error("error when depose ioc", e);
}
}
throw Lang.wrapThrow(e, LoadingException.class);
}
// ~ Done ^_^
sw.stop();
if (log.isInfoEnabled())
log.infof("Nutz.Mvc[%s] is up in %sms", config.getAppName(), sw.getDuration());
return mapping;
}
Aggregations