Search in sources :

Example 31 with Ioc

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());
}
Also used : Animal(org.nutz.ioc.json.pojo.Animal) Ioc(org.nutz.ioc.Ioc) NutIoc(org.nutz.ioc.impl.NutIoc) Test(org.junit.Test)

Example 32 with Ioc

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());
}
Also used : Animal(org.nutz.ioc.json.pojo.Animal) Ioc(org.nutz.ioc.Ioc) NutIoc(org.nutz.ioc.impl.NutIoc) Test(org.junit.Test)

Example 33 with Ioc

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);
}
Also used : Animal(org.nutz.ioc.json.pojo.Animal) Ioc(org.nutz.ioc.Ioc) NutIoc(org.nutz.ioc.impl.NutIoc) Test(org.junit.Test)

Example 34 with Ioc

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();
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) Bee(org.nutz.ioc.loader.xml.meta.Bee) Ioc(org.nutz.ioc.Ioc) NutIoc(org.nutz.ioc.impl.NutIoc) Test(org.junit.Test)

Example 35 with Ioc

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;
}
Also used : UrlMapping(org.nutz.mvc.UrlMapping) Stopwatch(org.nutz.lang.Stopwatch) LoadingException(org.nutz.mvc.LoadingException) Properties(java.util.Properties) Ioc(org.nutz.ioc.Ioc) File(java.io.File) LoadingException(org.nutz.mvc.LoadingException)

Aggregations

Ioc (org.nutz.ioc.Ioc)35 NutIoc (org.nutz.ioc.impl.NutIoc)29 Test (org.junit.Test)27 Animal (org.nutz.ioc.json.pojo.Animal)15 JsonLoader (org.nutz.ioc.loader.json.JsonLoader)6 Ioc2 (org.nutz.ioc.Ioc2)3 IocContext (org.nutz.ioc.IocContext)2 IocLoader (org.nutz.ioc.IocLoader)2 IocTO00 (org.nutz.ioc.json.pojo.IocTO00)2 MapLoader (org.nutz.ioc.loader.map.MapLoader)2 Stopwatch (org.nutz.lang.Stopwatch)2 LoadingException (org.nutz.mvc.LoadingException)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 HttpSession (javax.servlet.http.HttpSession)1 User (net.wendal.nutzdemo.bean.User)1 Dao (org.nutz.dao.Dao)1 ComboContext (org.nutz.ioc.impl.ComboContext)1