Search in sources :

Example 1 with IocMaking

use of org.nutz.ioc.IocMaking 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 2 with IocMaking

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

the class ChainParsingTest method test_constants_name.

@Test
public void test_constants_name() {
    String s = "@Name.substring(0, 6)";
    ChainNode cn = N(s);
    assertEquals(s, cn.toString());
    IocMaking ing = new IocMaking(null, null, null, null, null, "123456789");
    assertEquals("123456", cn.eval(ing));
}
Also used : IocMaking(org.nutz.ioc.IocMaking) Test(org.junit.Test)

Example 3 with IocMaking

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

the class InnerValue method get.

public Object get(IocMaking ing) {
    IocMaking innering = ing.clone(null);
    ObjectProxy op = ing.getObjectMaker().make(innering, iobj);
    return op.get(iobj.getType(), innering);
}
Also used : IocMaking(org.nutz.ioc.IocMaking) ObjectProxy(org.nutz.ioc.ObjectProxy)

Example 4 with IocMaking

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

the class ChainParsingTest method test_constants_context.

@Test
public void test_constants_context() {
    String s = "@Context.save('xx', 'tt', null)";
    ChainNode cn = N(s);
    assertEquals(s, cn.toString());
    IocMaking ing = new IocMaking(null, null, new ScopeContext("app"), null, null, null);
    assertFalse((Boolean) cn.eval(ing));
}
Also used : ScopeContext(org.nutz.ioc.impl.ScopeContext) IocMaking(org.nutz.ioc.IocMaking) Test(org.junit.Test)

Example 5 with IocMaking

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

the class NutIoc method get.

public <T> T get(Class<T> type, String name, IocContext context) throws IocException {
    if (log.isDebugEnabled())
        log.debugf("Get '%s'<%s>", name, type == null ? "" : type);
    try {
        if (this.mirrors instanceof LifeCycle)
            ((LifeCycle) this.mirrors).init();
    } catch (Exception e) {
        throw new IocException("_mirror_factory_init", e, "Mirror Factory init fail");
    }
    // 创建对象创建时
    IocMaking ing = makeIocMaking(context, name);
    IocContext cntx = ing.getContext();
    // 从上下文缓存中获取对象代理
    ObjectProxy op = cntx.fetch(name);
    // 如果未发现对象
    if (null == op) {
        // 线程同步
        synchronized (lock_get) {
            // 再次读取
            op = cntx.fetch(name);
            // 如果未发现对象
            if (null == op) {
                try {
                    if (log.isDebugEnabled())
                        log.debug("\t >> Load definition name=" + name);
                    // 读取对象定义
                    IocObject iobj = loader.load(createLoading(), name);
                    if (null == iobj) {
                        for (String iocBeanName : loader.getName()) {
                            // 感觉没必要..没有就没有呗
                            if (3 > LevenshteinDistance.computeLevenshteinDistance(name.toLowerCase(), iocBeanName.toLowerCase())) {
                                throw new IocException(name, "Undefined object '%s' but found similar name '%s'", name, iocBeanName);
                            }
                        }
                        throw new IocException(name, "Undefined object '%s'", name);
                    }
                    // 修正对象类型
                    if (null == iobj.getType())
                        if (null == type && Strings.isBlank(iobj.getFactory()))
                            throw new IocException(name, "NULL TYPE object '%s'", name);
                        else
                            iobj.setType(type);
                    // 检查对象级别
                    if (Strings.isBlank(iobj.getScope()))
                        iobj.setScope(defaultScope);
                    // 根据对象定义,创建对象,maker 会自动的缓存对象到 context 中
                    if (log.isDebugEnabled())
                        log.debugf("\t >> Make...'%s'<%s>", name, type == null ? "" : type);
                    op = maker.make(ing, iobj);
                }// 处理异常
                 catch (IocException e) {
                    ((IocException) e).addBeanNames(name);
                    throw e;
                } catch (Throwable e) {
                    throw new IocException(name, e, "For object [%s] - type:[%s]", name, type == null ? "" : type);
                }
            }
        }
    }
    synchronized (lock_get) {
        T re = op.get(type, ing);
        if (!name.startsWith("$") && re instanceof IocLoader) {
            loader.addLoader((IocLoader) re);
        }
        return re;
    }
}
Also used : LifeCycle(org.nutz.lang.util.LifeCycle) IocContext(org.nutz.ioc.IocContext) IocMaking(org.nutz.ioc.IocMaking) IocException(org.nutz.ioc.IocException) IocObject(org.nutz.ioc.meta.IocObject) ComboIocLoader(org.nutz.ioc.loader.combo.ComboIocLoader) IocLoader(org.nutz.ioc.IocLoader) ObjectProxy(org.nutz.ioc.ObjectProxy) IocException(org.nutz.ioc.IocException)

Aggregations

IocMaking (org.nutz.ioc.IocMaking)5 Test (org.junit.Test)3 ObjectProxy (org.nutz.ioc.ObjectProxy)2 Pet (org.nutz.dao.test.meta.Pet)1 Ioc2 (org.nutz.ioc.Ioc2)1 IocContext (org.nutz.ioc.IocContext)1 IocException (org.nutz.ioc.IocException)1 IocLoader (org.nutz.ioc.IocLoader)1 ValueProxy (org.nutz.ioc.ValueProxy)1 ValueProxyMaker (org.nutz.ioc.ValueProxyMaker)1 NutIoc (org.nutz.ioc.impl.NutIoc)1 ScopeContext (org.nutz.ioc.impl.ScopeContext)1 ComboIocLoader (org.nutz.ioc.loader.combo.ComboIocLoader)1 MapLoader (org.nutz.ioc.loader.map.MapLoader)1 IocObject (org.nutz.ioc.meta.IocObject)1 IocValue (org.nutz.ioc.meta.IocValue)1 LifeCycle (org.nutz.lang.util.LifeCycle)1