Browse Source

jfinal 3.5

James 7 years ago
parent
commit
5b431914fe

+ 18 - 0
src/main/java/com/jfinal/aop/Aop.java

@@ -169,6 +169,24 @@ public class Aop {
 		aopFactory.addMapping(from, to);
 	}
 	
+	/**
+	 * 功能与 addMapping(Class<T> from, Class<? extends T> to) 相同,仅仅是第二个参数
+	 * 由 Class 改为 String 类型,便于从外部配置文件传递 String 参数过来
+	 * 
+	 * <pre>
+	 * 示例:
+	 * Aop.addMapping(IService.class, "com.xxx.MyService")
+	 * 
+	 * 以上代码的参数 "com.xxx.MyService" 可通过外部配置文件传入,便于通过配置文件切换接口的
+	 * 实现类:
+	 * Aop.addMapping(IService.class, PropKit.get("ServiceImpl");
+	 * 
+	 * </pre>
+	 */
+	public static <T> void addMapping(Class<T> from, String to) {
+		aopFactory.addMapping(from, to);
+	}
+	
 	/* 通过 Aop.getAopFactory().inject(...) 可调用如下两个方法,不直接开放出来
 	public static void inject(Class<?> targetClass, Object targetObject) throws ReflectiveOperationException {
 		aopFactory.inject(targetClass, targetObject);

+ 15 - 0
src/main/java/com/jfinal/aop/AopFactory.java

@@ -76,6 +76,7 @@ public class AopFactory {
 		}
 	}
 	
+	// 方法原型的参数测试过可以是:Class<? super T> targetClass, T targetObject
 	public <T> T inject(Class<T> targetClass, T targetObject) throws ReflectiveOperationException {
 		inject(targetClass, targetObject, injectDepth);
 		return targetObject;
@@ -232,6 +233,20 @@ public class AopFactory {
 		return this;
 	}
 	
+	public <T> AopFactory addMapping(Class<T> from, String to) {
+		try {
+			@SuppressWarnings("unchecked")
+			Class<T> toClass = (Class<T>)Class.forName(to);
+			if (from.isAssignableFrom(toClass)) {
+				return addMapping(from, toClass);
+			} else {
+				throw new IllegalArgumentException("The parameter \"to\" must be the subclass or implementation of the parameter \"from\"");
+			}
+		} catch (ReflectiveOperationException e) {
+			throw new RuntimeException(e);
+		}
+	}
+	
 	/**
 	 * 获取父类到子类的映射值,或者接口到实现类的映射值
 	 * @param from 父类或者接口 

+ 1 - 1
src/main/java/com/jfinal/template/expr/ast/FieldKit.java

@@ -35,7 +35,7 @@ public class FieldKit {
 				fieldCache.put(key, field);
 			} else {
 				// 对于不存在的 Field,只进行一次获取操作,主要为了支持 null safe,未来需要考虑内存泄漏风险
-				fieldCache.put(key, Boolean.FALSE);
+				fieldCache.put(key, Void.class);
 			}
 		}
 		return field instanceof Field ? (Field)field : null;

+ 2 - 2
src/main/java/com/jfinal/template/expr/ast/MethodKit.java

@@ -112,7 +112,7 @@ public class MethodKit {
 				methodCache.put(key, method);
 			} else {
 				// 对于不存在的 Method,只进行一次获取操作,主要为了支持 null safe,未来需要考虑内存泄漏风险
-				methodCache.put(key, Boolean.FALSE);
+				methodCache.put(key, Void.class);
 			}
 		}
 		return method instanceof MethodInfo ? (MethodInfo)method : null;
@@ -129,7 +129,7 @@ public class MethodKit {
 			if (getterMethod != null) {
 				methodCache.put(key, getterMethod);
 			} else {
-				methodCache.put(key, Boolean.FALSE);
+				methodCache.put(key, Void.class);
 			}
 		}
 		return getterMethod instanceof MethodInfo ? (MethodInfo)getterMethod : null;

+ 1 - 1
src/main/java/com/jfinal/template/expr/ast/SharedMethodKit.java

@@ -56,7 +56,7 @@ public class SharedMethodKit {
 			if (method != null) {
 				methodCache.put(key, method);
 			}
-			// shared method 不支持 null safe,不缓存: methodCache.put(key, Boolean.FALSE)
+			// shared method 不支持 null safe,不缓存: methodCache.put(key, Void.class)
 		}
 		return method;
 	}