Skip to content

Commit

Permalink
重要修改: 不使用Ioc时,同一个Module改为使用同一个对象,而非现在的每个方法一个实例
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Jul 18, 2013
1 parent 77f0861 commit 25796e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/org/nutz/dao/impl/entity/NutEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @author zozoh(zozohtnt@gmail.com)
*/
public class NutEntity<T> implements Entity<T> {

private static final Object[] EMTRY_ARG = new Object[]{};

/**
* 按照 Java 字段名索引映射字段
Expand Down Expand Up @@ -198,7 +200,7 @@ public T getObject(ResultSet rs, FieldMatcher matcher) {
return bornByRS.born(Lang.array(rs));

// 通过反射每个字段逐次设置对象
T re = bornByDefault.born(new Object[]{});
T re = bornByDefault.born(EMTRY_ARG);
if (null == matcher)
for (MappingField fld : fields)
fld.injectValue(re, rs);
Expand All @@ -212,7 +214,7 @@ public T getObject(ResultSet rs, FieldMatcher matcher) {
}

public T getObject(Record rec) {
T obj = bornByDefault.born(new Object[]{});
T obj = bornByDefault.born(EMTRY_ARG);
for (MappingField fld : fields)
fld.injectValue(obj, rec);
return obj;
Expand Down
5 changes: 3 additions & 2 deletions src/org/nutz/mvc/impl/NutLoading.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ private static void createContext(NutConfig config) {
// 构建一个上下文对象,方便子类获取更多的环境信息
// 同时,所有 Filter 和 Adaptor 都可以用 ${app.root} 来填充自己
Context context = Lang.context();
context.set("app.root", config.getAppRoot());
String appRoot = config.getAppRoot();
context.set("app.root", appRoot);

if (log.isDebugEnabled()) {
log.debugf(">> app.root = %s", config.getAppRoot());
log.debugf(">> app.root = %s", appRoot);
}

// 载入环境变量
Expand Down
19 changes: 16 additions & 3 deletions src/org/nutz/mvc/impl/processor/ModuleProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.nutz.mvc.impl.processor;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.Ioc2;
Expand Down Expand Up @@ -33,16 +35,27 @@ public class ModuleProcessor extends AbstractProcessor {
private Class<?> moduleType;
private Method method;
private Object moduleObj;

private static Map<String, Object> modulesMap = new HashMap<String, Object>();

@Override
public void init(NutConfig config, ActionInfo ai) throws Throwable {
method = ai.getMethod();
moduleType = ai.getModuleType();
// 不使用 Ioc 容器管理模块
if (Strings.isBlank(ai.getInjectName())) {
if (log.isInfoEnabled())
log.info("Create Module obj without Ioc --> " + moduleType);
moduleObj = Mirror.me(moduleType).born();
// change in 1.b.49
// 同一个类的入口方法,共用同一个实例
synchronized (modulesMap) {
String className = moduleType.getName();
moduleObj = modulesMap.get(className);
if (moduleObj == null) {
if (log.isInfoEnabled())
log.info("Create Module obj without Ioc --> " + moduleType);
moduleObj = Mirror.me(moduleType).born();
modulesMap.put(className, moduleObj);
}
}
}
// 使用 Ioc 容器管理模块
else {
Expand Down

0 comments on commit 25796e4

Please sign in to comment.