Skip to content

Commit

Permalink
add: PropertiesIocLoader
Browse files Browse the repository at this point in the history
1. 通过Properties文件定义ioc bean
2. Iocs添加把单个对象包装为一个IocObject的方法
  • Loading branch information
wendal committed Jan 11, 2016
1 parent f6cbb5c commit 8ce01a9
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 44 deletions.
47 changes: 46 additions & 1 deletion src/org/nutz/ioc/Iocs.java
Expand Up @@ -6,6 +6,7 @@
import org.nutz.ioc.meta.IocEventSet;
import org.nutz.ioc.meta.IocField;
import org.nutz.ioc.meta.IocObject;
import org.nutz.ioc.meta.IocValue;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.meta.Pair;
Expand Down Expand Up @@ -79,10 +80,54 @@ public static IocObject mergeWith(IocObject me, IocObject it) {
me.copyArgys(it.getArgs());

// merge fields
for (IocField fld : it.getFields())
for (IocField fld : it.getFields().values())
if (!me.hasField(fld.getName()))
me.addField(fld);

return me;
}

public static IocValue convert(String value, boolean dftRefer) {
IocValue iocValue = new IocValue();
if (dftRefer && value.startsWith(":")) {
iocValue.setType(IocValue.TYPE_NORMAL);
iocValue.setValue(value.substring(1));
// } else if (value.startsWith("$")) {
// iocValue.setType(IocValue.TYPE_REFER);
// iocValue.setValue(value.substring(1));
}
else if (value.contains(":")) {
String type = value.substring(0, value.indexOf(':'));
if (IocValue.types.contains(type)) {
iocValue.setType(type);
if (value.endsWith(":")) {
iocValue.setValue("");
}
else
iocValue.setValue(value.substring(value.indexOf(':') + 1));
} else {
iocValue.setType(IocValue.TYPE_NORMAL);
iocValue.setValue(value);
}
} else {
// XXX 兼容性改变, 1.b.52 开始默认就是refer, 如果真的要输入常量
//log.info("auto set as refer:" + value);
iocValue.setType("refer");
iocValue.setValue(value);
}
return iocValue;
}

public static Object self(Object obj) {
return obj;
}

public static IocObject wrap(Object obj) {
IocObject iobj = new IocObject();
iobj.setType(obj.getClass());
iobj.setFactory(Iocs.class.getName() + ":self");
IocValue ival = new IocValue(IocValue.TYPE_NORMAL, obj);
iobj.addArg(ival);
return iobj;
}
}
9 changes: 6 additions & 3 deletions src/org/nutz/ioc/impl/ObjectMakerImpl.java
@@ -1,6 +1,8 @@
package org.nutz.ioc.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.nutz.ioc.IocEventTrigger;
import org.nutz.ioc.IocException;
Expand Down Expand Up @@ -114,9 +116,10 @@ public Object born(Object... args) {
}

// 获得每个字段的注入方式
FieldInjector[] fields = new FieldInjector[iobj.getFields().length];
List<IocField> _fields = new ArrayList<IocField>(iobj.getFields().values());
FieldInjector[] fields = new FieldInjector[_fields.size()];
for (int i = 0; i < fields.length; i++) {
IocField ifld = iobj.getFields()[i];
IocField ifld = _fields.get(i);
try {
ValueProxy vp = ing.makeValue(ifld.getValue());
fields[i] = FieldInjector.create(mirror, ifld.getName(), vp, ifld.isOptional());
Expand All @@ -139,7 +142,7 @@ public Object born(Object... args) {
catch (Throwable e) {
ing.getContext().remove(iobj.getScope(), ing.getObjectName());
String msg = e.getMessage();
if (msg.length() > 16*1024)
if (msg != null && msg.length() > 16*1024)
throw new IocException(e);
throw new IocException(e, "FAIL to create Ioc Bean name=[%s]", ing.getObjectName());
}
Expand Down
31 changes: 5 additions & 26 deletions src/org/nutz/ioc/loader/annotation/AnnotationIocLoader.java
Expand Up @@ -12,6 +12,7 @@
import org.nutz.ioc.IocException;
import org.nutz.ioc.IocLoader;
import org.nutz.ioc.IocLoading;
import org.nutz.ioc.Iocs;
import org.nutz.ioc.ObjectLoadException;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.meta.IocEventSet;
Expand Down Expand Up @@ -106,7 +107,7 @@ protected void addClass(Class<?> classZ) {
// args = iocBean.param();
if (null != args && args.length > 0)
for (String value : args)
iocObject.addArg(convert(value));
iocObject.addArg(Iocs.convert(value, true));

// 设置Events
IocEventSet eventSet = new IocEventSet();
Expand Down Expand Up @@ -135,7 +136,7 @@ protected void addClass(Class<?> classZ) {
iocValue.setType(IocValue.TYPE_REFER);
iocValue.setValue(field.getName());
} else
iocValue = convert(inject.value());
iocValue = Iocs.convert(inject.value(), true);
iocField.setValue(iocValue);
iocField.setOptional(inject.optional());
iocObject.addField(iocField);
Expand Down Expand Up @@ -181,7 +182,7 @@ protected void addClass(Class<?> classZ) {
iocValue.setType(IocValue.TYPE_REFER);
iocValue.setValue(Strings.lowerFirst(methodName.substring(3)));
} else
iocValue = convert(inject.value());
iocValue = Iocs.convert(inject.value(), true);
iocField.setValue(iocValue);
iocObject.addField(iocField);
fieldList.add(iocField.getName());
Expand All @@ -198,7 +199,7 @@ protected void addClass(Class<?> classZ) {
String[] datas = fieldInfo.split(":", 2);
// 完整形式, 与@Inject完全一致了
iocField.setName(datas[0]);
iocField.setValue(convert(datas[1]));
iocField.setValue(Iocs.convert(datas[1], true));
iocObject.addField(iocField);
} else {
// 基本形式, 引用与自身同名的bean
Expand Down Expand Up @@ -237,28 +238,6 @@ protected void addClass(Class<?> classZ) {
}
}

protected IocValue convert(String value) {
IocValue iocValue = new IocValue();
if (value.startsWith(":")) {
iocValue.setType(IocValue.TYPE_NORMAL);
iocValue.setValue(value);
}
else if (value.contains(":")) {
iocValue.setType(value.substring(0, value.indexOf(':')));
if (value.endsWith(":")) {
iocValue.setValue("");
}
else
iocValue.setValue(value.substring(value.indexOf(':') + 1));
} else {
// XXX 兼容性改变, 1.b.52 开始默认就是refer, 如果真的要输入常量
log.info("auto set as refer:" + value);
iocValue.setType("refer");
iocValue.setValue(value);
}
return iocValue;
}

public String[] getName() {
return map.keySet().toArray(new String[map.size()]);
}
Expand Down
3 changes: 3 additions & 0 deletions src/org/nutz/ioc/loader/combo/ComboIocLoader.java
Expand Up @@ -14,6 +14,7 @@
import org.nutz.ioc.ObjectLoadException;
import org.nutz.ioc.loader.annotation.AnnotationIocLoader;
import org.nutz.ioc.loader.json.JsonLoader;
import org.nutz.ioc.loader.properties.PropertiesIocLoader;
import org.nutz.ioc.loader.xml.XmlIocLoader;
import org.nutz.ioc.meta.IocObject;
import org.nutz.json.Json;
Expand Down Expand Up @@ -66,6 +67,8 @@ public ComboIocLoader(String... args) throws ClassNotFoundException {
loaders.put("anno", AnnotationIocLoader.class);
loaders.put("trans", TransIocLoader.class);
loaders.put("tx", TransIocLoader.class);
loaders.put("props", PropertiesIocLoader.class);
loaders.put("properties", PropertiesIocLoader.class);
try {
loaders.put("cache",
(Class<? extends IocLoader>) Lang.loadClass("org.nutz.jcache.NutCacheIocLoader"));
Expand Down
81 changes: 81 additions & 0 deletions src/org/nutz/ioc/loader/properties/PropertiesIocLoader.java
@@ -0,0 +1,81 @@
package org.nutz.ioc.loader.properties;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.ioc.IocLoader;
import org.nutz.ioc.IocLoading;
import org.nutz.ioc.Iocs;
import org.nutz.ioc.ObjectLoadException;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.meta.IocObject;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mapl.Mapl;
import org.nutz.mvc.adaptor.ParamExtractor;
import org.nutz.mvc.adaptor.Params;
import org.nutz.mvc.adaptor.injector.ObjectNaviNode;

public class PropertiesIocLoader extends PropertiesProxy implements IocLoader {

private static Log log = Logs.get();

protected Map<String, IocObject> objs = new HashMap<String, IocObject>();

public PropertiesIocLoader() {}

public PropertiesIocLoader(String...paths) {
super(paths);
log.debug("beans = " + objs.keySet());
}

public String[] getName() {
reload();
return objs.keySet().toArray(new String[objs.size()]);
}

public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
reload();
return objs.get(name);
}

public boolean has(String name) {
reload();
return objs.containsKey(name);
}

@SuppressWarnings("rawtypes")
public void reload() {
List<String> beanNames = new ArrayList<String>();
for (String key : keys()) {
if (!key.startsWith("ioc.") || key.length() < 5)
continue;
String[] tmp = key.split("[.]");
if (tmp.length == 3) {
if (tmp[2].equals("type") || tmp[2].equals("factory")) {
beanNames.add(tmp[1]);
}
}
}
for (String beanName : beanNames) {
ObjectNaviNode no = new ObjectNaviNode();
String prefix = "ioc." + beanName+".";
String pre = "";
ParamExtractor pe = Params.makeParamExtractor(null, this.toMap());
for (Object name : pe.keys()) {
String na = (String) name;
if (na.startsWith(prefix)) {
no.put(pre + na, pe.extractor(na));
}
}
Object model = no.get();
Object re = Mapl.maplistToObj(((Map)model).get(beanName), IocObject.class);
this.objs.put(beanName, (IocObject) re);
}

// 插入自身
this.objs.put("conf", Iocs.wrap(this));
}
}
27 changes: 27 additions & 0 deletions src/org/nutz/ioc/meta/IocField.java
@@ -1,6 +1,9 @@
package org.nutz.ioc.meta;

import org.nutz.ioc.Iocs;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;

/**
* 描述了一个对象的字段,两个属性分别表示字段名,和字段值
Expand All @@ -16,6 +19,19 @@ public class IocField {
private IocValue value;

private boolean optional;

public IocField() {}

public IocField(String value) {
if (value.contains(":")) {
this.value = Iocs.convert(value, false);
} else {
IocValue tmp = new IocValue();
tmp.setType(IocValue.TYPE_NORMAL);
tmp.setValue(value);
this.value = tmp;
}
}

public String getName() {
return name;
Expand Down Expand Up @@ -45,4 +61,15 @@ public boolean isOptional() {
public void setOptional(boolean optional) {
this.optional = optional;
}

public String toJson(JsonFormat jf) {
if (!optional)
return Json.toJson(this.value, jf);
else{
NutMap map = new NutMap();
map.put("optional", optional);
map.put(this.value.getType(), this.value.getValue());
return Json.toJson(map, jf);
}
}
}
17 changes: 8 additions & 9 deletions src/org/nutz/ioc/meta/IocObject.java
@@ -1,7 +1,9 @@
package org.nutz.ioc.meta;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.nutz.json.Json;

Expand Down Expand Up @@ -46,7 +48,7 @@ public class IocObject implements Cloneable {
/**
* 对象的字段
*/
private List<IocField> fields;
private Map<String, IocField> fields;

/**
* 对象基本,容器根据这个字段,来决定将这个对象保存在哪一个上下文范围中<br>
Expand All @@ -58,7 +60,7 @@ public class IocObject implements Cloneable {

public IocObject() {
args = new ArrayList<IocValue>();
fields = new ArrayList<IocField>();
fields = new LinkedHashMap<String, IocField>();
singleton = true;
}

Expand Down Expand Up @@ -113,19 +115,16 @@ public void copyArgys(IocValue[] args) {
}
}

public IocField[] getFields() {
return fields.toArray(new IocField[fields.size()]);
public Map<String, IocField> getFields() {
return fields;
}

public void addField(IocField field) {
this.fields.add(field);
this.fields.put(field.getName(), field);
}

public boolean hasField(String name) {
for (IocField fld : fields)
if (fld.getName().equals(name))
return true;
return false;
return fields.containsKey(name);
}

public IocObject clone() {
Expand Down

0 comments on commit 8ce01a9

Please sign in to comment.