Search in sources :

Example 6 with IocContext

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

the class NutIoc method getNamesByType.

public String[] getNamesByType(Class<?> klass, IocContext context) {
    List<String> names = new ArrayList<String>();
    for (String name : getNames()) {
        try {
            IocObject iobj = loader.load(createLoading(), name);
            if (iobj != null && iobj.getType() != null && klass.isAssignableFrom(iobj.getType()))
                names.add(name);
        } catch (Exception e) {
        }
    }
    IocContext cntx;
    if (null == context || context == this.context)
        cntx = this.context;
    else
        cntx = new ComboContext(context, this.context);
    for (String name : cntx.names()) {
        ObjectProxy op = cntx.fetch(name);
        if (op.getObj() != null && klass.isAssignableFrom(op.getObj().getClass()))
            names.add(name);
    }
    return new LinkedHashSet<String>(names).toArray(new String[names.size()]);
}
Also used : IocContext(org.nutz.ioc.IocContext) ArrayList(java.util.ArrayList) IocObject(org.nutz.ioc.meta.IocObject) ObjectProxy(org.nutz.ioc.ObjectProxy) IocException(org.nutz.ioc.IocException)

Example 7 with IocContext

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

the class NutIoc method getByType.

public <K> K getByType(Class<K> klass, IocContext context) {
    String _name = null;
    IocContext cntx;
    if (null == context || context == this.context)
        cntx = this.context;
    else
        cntx = new ComboContext(context, this.context);
    for (String name : cntx.names()) {
        ObjectProxy op = cntx.fetch(name);
        if (op.getObj() != null && klass.isAssignableFrom(op.getObj().getClass())) {
            _name = name;
            break;
        }
    }
    if (_name != null)
        return get(klass, _name, context);
    for (String name : getNames()) {
        try {
            IocObject iobj = loader.load(createLoading(), name);
            if (iobj != null && iobj.getType() != null && klass.isAssignableFrom(iobj.getType()))
                _name = name;
        } catch (Exception e) {
            continue;
        }
        if (_name != null)
            return get(klass, name, context);
    }
    throw new IocException("class:" + klass.getName(), "none ioc bean match class=" + klass.getName());
}
Also used : IocContext(org.nutz.ioc.IocContext) IocObject(org.nutz.ioc.meta.IocObject) IocException(org.nutz.ioc.IocException) ObjectProxy(org.nutz.ioc.ObjectProxy) IocException(org.nutz.ioc.IocException)

Aggregations

IocContext (org.nutz.ioc.IocContext)7 Ioc2 (org.nutz.ioc.Ioc2)4 IocException (org.nutz.ioc.IocException)3 ObjectProxy (org.nutz.ioc.ObjectProxy)3 IocObject (org.nutz.ioc.meta.IocObject)3 Test (org.junit.Test)2 Ioc (org.nutz.ioc.Ioc)2 ComboContext (org.nutz.ioc.impl.ComboContext)2 NutIoc (org.nutz.ioc.impl.NutIoc)2 ScopeContext (org.nutz.ioc.impl.ScopeContext)2 MapLoader (org.nutz.ioc.loader.map.MapLoader)2 ArrayList (java.util.ArrayList)1 HttpSession (javax.servlet.http.HttpSession)1 IocLoader (org.nutz.ioc.IocLoader)1 IocMaking (org.nutz.ioc.IocMaking)1 ComboIocLoader (org.nutz.ioc.loader.combo.ComboIocLoader)1 LifeCycle (org.nutz.lang.util.LifeCycle)1 RequestIocContext (org.nutz.mvc.ioc.RequestIocContext)1 SessionIocContext (org.nutz.mvc.ioc.SessionIocContext)1