Search in sources :

Example 6 with IocLoader

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

the class SimpleJsonIocTest method test_load_from_dir.

@Test
public void test_load_from_dir() throws ObjectLoadException {
    IocLoader loader = new JsonLoader("org/nutz/ioc/json/");
    assertTrue(loader.getName().length > 0);
}
Also used : IocLoader(org.nutz.ioc.IocLoader) JsonLoader(org.nutz.ioc.loader.json.JsonLoader) Test(org.junit.Test)

Example 7 with IocLoader

use of org.nutz.ioc.IocLoader 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)

Example 8 with IocLoader

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

the class ComboIocLoader method load.

public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
    for (IocLoader iocLoader : iocLoaders) if (iocLoader.has(name)) {
        IocObject iocObject = iocLoader.load(loading, name);
        if (log.isDebugEnabled()) {
            // TODO 弄成更好看的格式,方便debug
            String printName;
            if (iocLoader instanceof AnnotationIocLoader) {
                String packages = Arrays.toString(((AnnotationIocLoader) iocLoader).getPackages());
                printName = "AnnotationIocLoader(packages=" + packages + ")";
            } else if (JsonLoader.class.equals(iocLoader.getClass()) && ((JsonLoader) iocLoader).getPaths() != null) {
                String paths = Arrays.toString(((JsonLoader) iocLoader).getPaths());
                printName = "JsonLoader(paths=" + paths + ")";
            } else {
                printName = iocLoader.getClass().getSimpleName() + "@" + iocLoader.hashCode();
            }
            log.debugf("Found IocObject(%s) in %s", name, printName);
        }
        return iocObject;
    }
    throw new ObjectLoadException("Object '" + name + "' without define!");
}
Also used : AnnotationIocLoader(org.nutz.ioc.loader.annotation.AnnotationIocLoader) ObjectLoadException(org.nutz.ioc.ObjectLoadException) IocObject(org.nutz.ioc.meta.IocObject) XmlIocLoader(org.nutz.ioc.loader.xml.XmlIocLoader) IocLoader(org.nutz.ioc.IocLoader) AsyncAopIocLoader(org.nutz.aop.interceptor.async.AsyncAopIocLoader) AnnotationIocLoader(org.nutz.ioc.loader.annotation.AnnotationIocLoader) PropertiesIocLoader(org.nutz.ioc.loader.properties.PropertiesIocLoader) TransIocLoader(org.nutz.aop.interceptor.ioc.TransIocLoader) JsonLoader(org.nutz.ioc.loader.json.JsonLoader)

Aggregations

IocLoader (org.nutz.ioc.IocLoader)8 Test (org.junit.Test)5 JsonLoader (org.nutz.ioc.loader.json.JsonLoader)5 IocObject (org.nutz.ioc.meta.IocObject)3 AsyncAopIocLoader (org.nutz.aop.interceptor.async.AsyncAopIocLoader)2 TransIocLoader (org.nutz.aop.interceptor.ioc.TransIocLoader)2 Ioc (org.nutz.ioc.Ioc)2 NutIoc (org.nutz.ioc.impl.NutIoc)2 AnnotationIocLoader (org.nutz.ioc.loader.annotation.AnnotationIocLoader)2 PropertiesIocLoader (org.nutz.ioc.loader.properties.PropertiesIocLoader)2 XmlIocLoader (org.nutz.ioc.loader.xml.XmlIocLoader)2 IocContext (org.nutz.ioc.IocContext)1 IocException (org.nutz.ioc.IocException)1 IocMaking (org.nutz.ioc.IocMaking)1 ObjectLoadException (org.nutz.ioc.ObjectLoadException)1 ObjectProxy (org.nutz.ioc.ObjectProxy)1 Mammal (org.nutz.ioc.json.pojo.Mammal)1 ComboIocLoader (org.nutz.ioc.loader.combo.ComboIocLoader)1 IocField (org.nutz.ioc.meta.IocField)1 IocValue (org.nutz.ioc.meta.IocValue)1