Search in sources :

Example 1 with Ioc2

use of org.nutz.ioc.Ioc2 in project nutz by nutzam.

the class DefaultValueTypes method test_el.

@Test
public void test_el() {
    IocContext context = new ScopeContext("abc");
    String json = "{obj:{type:'org.nutz.ioc.val.DefaultValueTypes', fields:{name:{el:'sys[\"os.arch\"]'}}}}";
    System.out.println(Json.toJson(Json.fromJson(json)));
    Ioc2 ioc = new NutIoc(new MapLoader(json), context, "abc");
    DefaultValueTypes self = ioc.get(DefaultValueTypes.class, "obj");
    assertEquals(System.getProperties().get("os.arch"), self.name);
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) IocContext(org.nutz.ioc.IocContext) ScopeContext(org.nutz.ioc.impl.ScopeContext) MapLoader(org.nutz.ioc.loader.map.MapLoader) Ioc2(org.nutz.ioc.Ioc2) Test(org.junit.Test)

Example 2 with Ioc2

use of org.nutz.ioc.Ioc2 in project nutz by nutzam.

the class IocCustomizedValueTypeTest method test_simple_customized.

@Test
public void test_simple_customized() {
    String json = "{xb:{name:{cc:'XiaoBai'}}}";
    Ioc2 ioc = new NutIoc(new MapLoader(json));
    ioc.addValueProxyMaker(new ValueProxyMaker() {

        public ValueProxy make(IocMaking ing, IocValue iv) {
            if ("cc".equalsIgnoreCase(iv.getType())) {
                return new StaticValue("CC:" + iv.getValue());
            }
            return null;
        }

        public String[] supportedTypes() {
            return Lang.array("cc");
        }
    });
    Pet pet = ioc.get(Pet.class, "xb");
    assertEquals("CC:XiaoBai", pet.getName());
    ioc.depose();
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) MapLoader(org.nutz.ioc.loader.map.MapLoader) IocMaking(org.nutz.ioc.IocMaking) ValueProxy(org.nutz.ioc.ValueProxy) ValueProxyMaker(org.nutz.ioc.ValueProxyMaker) IocValue(org.nutz.ioc.meta.IocValue) Ioc2(org.nutz.ioc.Ioc2) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 3 with Ioc2

use of org.nutz.ioc.Ioc2 in project nutz by nutzam.

the class ModuleProcessor method process.

public void process(ActionContext ac) throws Throwable {
    RequestIocContext reqContext = null;
    try {
        if (null != moduleObj) {
            ac.setModule(moduleObj);
        } else {
            Ioc ioc = ac.getIoc();
            Object obj;
            /*
                 * 如果 Ioc 容器实现了高级接口,那么会为当前请求设置上下文对象
                 */
            if (NutSessionListener.isSessionScopeEnable && ioc instanceof Ioc2) {
                reqContext = new RequestIocContext(ac.getRequest());
                HttpSession sess = Mvcs.getHttpSession(false);
                IocContext myContext = null;
                // 如果容器可以创建 Session ...
                if (null != sess) {
                    SessionIocContext sessionContext = new SessionIocContext(sess);
                    myContext = new ComboContext(reqContext, sessionContext);
                } else // 如果容器禁止了 Session ...
                {
                    myContext = reqContext;
                }
                Mvcs.setIocContext(myContext);
                obj = ((Ioc2) ioc).get(moduleType, injectName, myContext);
            } else
                /*
                 * 否则,则仅仅简单的从容器获取
                 */
                obj = ioc.get(moduleType, injectName);
            ac.setModule(obj);
        }
        ac.setMethod(method);
        // if (log.isDebugEnabled()) //打印实际执行的Method信息
        // log.debugf("Handle URL[%s] by Method[%s]",ac.getPath(),method);
        doNext(ac);
    } finally {
        if (reqContext != null)
            try {
                reqContext.depose();
            } catch (Throwable e) {
                if (log.isDebugEnabled())
                    log.debug("ReqContext depose fail?!", e);
            }
    }
}
Also used : ComboContext(org.nutz.ioc.impl.ComboContext) SessionIocContext(org.nutz.mvc.ioc.SessionIocContext) RequestIocContext(org.nutz.mvc.ioc.RequestIocContext) IocContext(org.nutz.ioc.IocContext) SessionIocContext(org.nutz.mvc.ioc.SessionIocContext) HttpSession(javax.servlet.http.HttpSession) Ioc2(org.nutz.ioc.Ioc2) Ioc(org.nutz.ioc.Ioc) RequestIocContext(org.nutz.mvc.ioc.RequestIocContext)

Example 4 with Ioc2

use of org.nutz.ioc.Ioc2 in project nutz by nutzam.

the class NutLoading method createIoc.

protected Ioc createIoc(NutConfig config, Class<?> mainModule) throws Exception {
    IocBy ib = mainModule.getAnnotation(IocBy.class);
    if (null != ib) {
        if (log.isDebugEnabled())
            log.debugf("@IocBy(type=%s, args=%s,init=%s)", ib.type().getName(), Json.toJson(ib.args()), Json.toJson(ib.init()));
        Ioc ioc = Mirror.me(ib.type()).born().create(config, ib.args());
        // 如果是 Ioc2 的实现,增加新的 ValueMaker
        if (ioc instanceof Ioc2) {
            ((Ioc2) ioc).addValueProxyMaker(new ServletValueProxyMaker(config.getServletContext()));
        }
        // 如果给定了 Ioc 的初始化,则依次调用
        for (String objName : ib.init()) {
            ioc.get(null, objName);
        }
        // 保存 Ioc 对象
        Mvcs.setIoc(ioc);
        return ioc;
    } else if (log.isInfoEnabled())
        log.info("!!!Your application without @IocBy supporting");
    return null;
}
Also used : Ioc2(org.nutz.ioc.Ioc2) IocBy(org.nutz.mvc.annotation.IocBy) Ioc(org.nutz.ioc.Ioc)

Example 5 with Ioc2

use of org.nutz.ioc.Ioc2 in project nutz by nutzam.

the class ReferTypeValue method get.

public Object get(IocMaking ing) {
    Ioc ioc = ing.getIoc();
    IocContext ctx = ing.getContext();
    if (typeFirst) {
        String[] names;
        if (ioc instanceof Ioc2) {
            names = ((Ioc2) ioc).getNamesByType(type, ctx);
            if (names != null && names.length == 1) {
                return ((Ioc2) ioc).get(type, names[0], ctx);
            }
        } else {
            names = ioc.getNamesByType(type);
            if (names != null && names.length == 1) {
                return ioc.get(type, names[0]);
            }
        }
    }
    if (ioc.has(name)) {
        if (ioc instanceof Ioc2)
            return ((Ioc2) ioc).get(type, name, ctx);
        return ioc.get(type, name);
    }
    if (log.isDebugEnabled())
        log.debugf("name=%s not found, search for type=%s", name, type.getName());
    if (ioc instanceof Ioc2)
        return ((Ioc2) ioc).getByType(type, ctx);
    else
        return ioc.getByType(type);
}
Also used : IocContext(org.nutz.ioc.IocContext) Ioc2(org.nutz.ioc.Ioc2) Ioc(org.nutz.ioc.Ioc)

Aggregations

Ioc2 (org.nutz.ioc.Ioc2)8 Test (org.junit.Test)5 IocContext (org.nutz.ioc.IocContext)4 Ioc (org.nutz.ioc.Ioc)3 NutIoc (org.nutz.ioc.impl.NutIoc)3 ScopeContext (org.nutz.ioc.impl.ScopeContext)3 MapLoader (org.nutz.ioc.loader.map.MapLoader)3 ComboContext (org.nutz.ioc.impl.ComboContext)2 HttpSession (javax.servlet.http.HttpSession)1 Pet (org.nutz.dao.test.meta.Pet)1 IocMaking (org.nutz.ioc.IocMaking)1 ObjectProxy (org.nutz.ioc.ObjectProxy)1 ValueProxy (org.nutz.ioc.ValueProxy)1 ValueProxyMaker (org.nutz.ioc.ValueProxyMaker)1 Animal (org.nutz.ioc.json.pojo.Animal)1 IocValue (org.nutz.ioc.meta.IocValue)1 IocBy (org.nutz.mvc.annotation.IocBy)1 RequestIocContext (org.nutz.mvc.ioc.RequestIocContext)1 SessionIocContext (org.nutz.mvc.ioc.SessionIocContext)1