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);
}
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();
}
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);
}
}
}
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;
}
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);
}
Aggregations