Browse Source

jfinal 4.8

James 6 years ago
parent
commit
e7efc88a2b

+ 14 - 2
src/main/java/com/jfinal/aop/AopFactory.java

@@ -217,7 +217,10 @@ public class AopFactory {
 		return injectSuperClass;
 	}
 	
-	public AopFactory addSingletonObject(Object singletonObject) {
+	public AopFactory addSingletonObject(Class<?> type, Object singletonObject) {
+		if (type == null) {
+			throw new IllegalArgumentException("type can not be null");
+		}
 		if (singletonObject == null) {
 			throw new IllegalArgumentException("singletonObject can not be null");
 		}
@@ -225,7 +228,11 @@ public class AopFactory {
 			throw new IllegalArgumentException("singletonObject can not be Class type");
 		}
 		
-		Class<?> type = getUsefulClass(singletonObject.getClass());
+		if ( ! (type.isAssignableFrom(singletonObject.getClass())) ) {
+			throw new IllegalArgumentException(singletonObject.getClass().getName() + " can not cast to " + type.getName());
+		}
+		
+		// Class<?> type = getUsefulClass(singletonObject.getClass());
 		if (singletonCache.putIfAbsent(type, singletonObject) != null) {
 			throw new RuntimeException("Singleton object already exists for type : " + type.getName());
 		}
@@ -233,6 +240,11 @@ public class AopFactory {
 		return this;
 	}
 	
+	public AopFactory addSingletonObject(Object singletonObject) {
+		Class<?> type = getUsefulClass(singletonObject.getClass());
+		return addSingletonObject(type, singletonObject);
+	}
+	
 	public synchronized <T> AopFactory addMapping(Class<T> from, Class<? extends T> to) {
 		if (from == null || to == null) {
 			throw new IllegalArgumentException("The parameter from and to can not be null");

+ 6 - 2
src/main/java/com/jfinal/aop/AopManager.java

@@ -73,7 +73,7 @@ public class AopManager {
 	 * 示例:
 	 * // Service 类的构造方法中传入了两个参数
 	 * Service service = new Service(paraAaa, paraBbb);
-	 * AopManager.me().addSingletonObject(service);
+	 * AopManager.me().addSingletonObject(Service.class, service);
 	 * 
 	 * // 上面代码添加完成以后,可以在任何地方通过下面的方式获取单例对象 
 	 * service = Aop.get(Service.class);
@@ -85,9 +85,13 @@ public class AopManager {
 	 * // 在添加为单例对象之前还可以先为其注入依赖对象
 	 * Service service = new Service(paraAaa, paraBbb);
 	 * Aop.inject(service);		// 这里是对 Service 进行依赖注入
-	 * AopManager.me().addSingletonObject(service);
+	 * AopManager.me().addSingletonObject(Service.class, service);
 	 * </pre>
 	 */
+	public void addSingletonObject(Class<?> type, Object singletonObject) {
+		Aop.aopFactory.addSingletonObject(type, singletonObject);
+	}
+	
 	public void addSingletonObject(Object singletonObject) {
 		Aop.aopFactory.addSingletonObject(singletonObject);
 	}