Browse Source

改注释

James 1 year ago
parent
commit
99b6711ade

+ 23 - 23
src/main/java/com/jfinal/aop/Aop.java

@@ -18,85 +18,85 @@ package com.jfinal.aop;
 
 /**
  * Aop 支持在任意时空便捷使用 Aop
- * 
+ *
  * Aop 主要功能:
  * 1:Aop.get(Class) 根据 Class 去创建对象,然后对创建好的对象进行依赖注入
- * 
+ *
  * 2:Aop.inject(Object) 对传入的对象进行依赖注入
- * 
+ *
  * 3:Aop.inject(...) 与 Aop.get(...) 的区别是前者只针对传入的对象之中的属性进行注入。
  *    而后者先要使用 Class 去创建对象,创建完对象以后对该对象之中的属性进行注入。
  *    简单一句话:get(...) 比 inject(...) 多了一个目标对象的创建过程
- *    
+ *
  * 4:AopManager.me().setSingleton(...) 用于配置默认是否为单例
- * 
+ *
  * 5:在目标类上使用注解 Singleton 可以覆盖掉上面 setSingleton(...) 方法配置的默认值
- * 
- * 
+ *
+ *
  * 基本用法:
  * 1:先定义业务
  *    public class Service {
  *       @Inject
  *       OtherService otherSrv;
- *       
+ *
  *       @Before(Aaa.class)
  *       public void doIt() {
  *          ...
  *       }
  *    }
- *    
+ *
  *    public class OtherService {
  *       @Before(Bbb.class)
  *       public void doOther() {
  *          ...
  *       }
  *    }
- *    
- * 
+ *
+ *
  * 2:只进行注入,对象自己创建
  *    Service srv = Aop.inject(new Service());
  *    srv.doIt();
  *    由于 Service 对象是 new 出来的,不会被 AOP 代理,所以其 doIt() 方法上的 Aaa 拦截器并不会生效
  *    Aop.inject(...) 会对 OtherService otherSrv 进行注入,并且对 otherSrv 进行 AOP 代理,
  *    所以 OtherService.doOther() 方法上的 Bbb 拦截器会生效
- *    
+ *
  * 3:创建对象并注入
  *    Service srv = Aop.get(Service.class);
  *    srv.doIt();
  *    Aop.get(...) 用法对 OtherService otherSrv 的处理方式完全一样,在此基础之上 Service 自身也会被
  *    AOP 代理,所以 doIt() 上的 Aaa 拦截器会生效
- * 
+ *
  * 4:小结:对象的创建交给 Aop 而不是自己 new 出来,所创建的对象才能被 AOP 代理,其上的拦截器才能生效
- * 
- * 
+ *
+ *
  * 高级用法:
  * 1:@Inject 注解默认注入属性自身类型的对象,可以通过如下代码指定被注入的类型:
  *    @Inject(UserServiceImpl.class)			// 此处的 UserServiceImpl 为 UserService 的子类或实现类
  *    UserService userService;
- * 
+ *
  * 2:被注入对象默认是 singleton 单例,可以通过 AopManager.me().setSingleton(false) 配置默认不为单例
- * 
+ *
  * 3:可以在目标类中中直接配置注解 Singleton:
  *    @Singleton(false)
  *    public class MyService {...}
- *    
+ *
  *    注意:以上代码中的注解会覆盖掉 2 中 setSingleton() 方法配置的默认值
- * 
+ *
  * 4:如上 2、3 中的配置,建议的用法是:先用 setSingleton() 配置大多数情况,然后在个别
  *    违反上述配置的情况下使用 Singleton 注解来覆盖默认配置,这样可以节省大量代码
  */
 public class Aop {
-	
+
 	static AopFactory aopFactory = new AopFactory();
-	
+
 	public static <T> T get(Class<T> targetClass) {
 		return aopFactory.get(targetClass);
 	}
-	
+
 	public static <T> T inject(T targetObject) {
 		return aopFactory.inject(targetObject);
 	}
-	
+
 	/* 通过 AopManager.me().getAopFactory().inject(...) 可调用如下方法,不直接开放出来
 	public static <T> T inject(Class<T> targetClass, T targetObject) {
 		return aopFactory.inject(targetClass, targetObject);

+ 41 - 41
src/main/java/com/jfinal/aop/AopFactory.java

@@ -28,21 +28,21 @@ import com.jfinal.validate.Validator;
  * AopFactory 是工具类 Aop 功能的具体实现,详细用法见 Aop
  */
 public class AopFactory {
-	
+
 	// 单例缓存
 	protected ConcurrentHashMap<Class<?>, Object> singletonCache = new ConcurrentHashMap<Class<?>, Object>();
-	
+
 	// 支持循环注入
 	protected ThreadLocal<HashMap<Class<?>, Object>> singletonTl = ThreadLocal.withInitial(() -> new HashMap<>());
 	protected ThreadLocal<HashMap<Class<?>, Object>> prototypeTl = ThreadLocal.withInitial(() -> new HashMap<>());
-	
+
 	// 父类到子类、接口到实现类之间的映射关系
 	protected HashMap<Class<?>, Class<?>> mapping = null;
-	
+
 	protected boolean singleton = true;					// 默认单例
-	
+
 	protected boolean injectSuperClass = false;			// 默认不对超类进行注入
-	
+
 	public <T> T get(Class<T> targetClass) {
 		try {
 			return doGet(targetClass);
@@ -50,31 +50,31 @@ public class AopFactory {
 			throw new RuntimeException(e);
 		}
 	}
-	
+
 	@SuppressWarnings("unchecked")
 	protected <T> T doGet(Class<T> targetClass) throws ReflectiveOperationException {
 		// Aop.get(obj.getClass()) 可以用 Aop.inject(obj),所以注掉下一行代码
 		// targetClass = (Class<T>)getUsefulClass(targetClass);
-		
+
 		targetClass = (Class<T>)getMappingClass(targetClass);
-		
+
 		Singleton si = targetClass.getAnnotation(Singleton.class);
 		boolean singleton = (si != null ? si.value() : this.singleton);
-		
+
 		if (singleton) {
 			return doGetSingleton(targetClass);
 		} else {
 			return doGetPrototype(targetClass);
 		}
 	}
-	
+
 	@SuppressWarnings("unchecked")
 	protected <T> T doGetSingleton(Class<T> targetClass) throws ReflectiveOperationException {
 		Object ret = singletonCache.get(targetClass);
 		if (ret != null) {
 			return (T)ret;
 		}
-		
+
 		HashMap<Class<?>, Object> map = singletonTl.get();
 		int size = map.size();
 		if (size > 0) {
@@ -83,7 +83,7 @@ public class AopFactory {
 				return (T)ret;
 			}
 		}
-		
+
 		synchronized (this) {
 			try {
 				ret = singletonCache.get(targetClass);
@@ -101,11 +101,11 @@ public class AopFactory {
 			}
 		}
 	}
-	
+
 	@SuppressWarnings("unchecked")
 	protected <T> T doGetPrototype(Class<T> targetClass) throws ReflectiveOperationException {
 		Object ret;
-		
+
 		HashMap<Class<?>, Object> map = prototypeTl.get();
 		int size = map.size();
 		if (size > 0) {
@@ -115,7 +115,7 @@ public class AopFactory {
 				return (T)createObject(targetClass);
 			}
 		}
-		
+
 		try {
 			ret = createObject(targetClass);
 			map.put(targetClass, ret);
@@ -127,7 +127,7 @@ public class AopFactory {
 			}
 		}
 	}
-	
+
 	public <T> T inject(T targetObject) {
 		try {
 			doInject(targetObject.getClass(), targetObject);
@@ -136,7 +136,7 @@ public class AopFactory {
 			throw new RuntimeException(e);
 		}
 	}
-	
+
 	// 方法原型的参数测试过可以是:Class<? super T> targetClass, T targetObject
 	public <T> T inject(Class<T> targetClass, T targetObject) {
 		try {
@@ -146,7 +146,7 @@ public class AopFactory {
 			throw new RuntimeException(e);
 		}
 	}
-	
+
 	protected void doInject(Class<?> targetClass, Object targetObject) throws ReflectiveOperationException {
 		targetClass = getUsefulClass(targetClass);
 		Field[] fields = targetClass.getDeclaredFields();
@@ -156,18 +156,18 @@ public class AopFactory {
 				if (inject == null) {
 					continue ;
 				}
-				
+
 				Class<?> fieldInjectedClass = inject.value();
 				if (fieldInjectedClass == Void.class) {
 					fieldInjectedClass = field.getType();
 				}
-				
+
 				Object fieldInjectedObject = doGet(fieldInjectedClass);
 				field.setAccessible(true);
 				field.set(targetObject, fieldInjectedObject);
 			}
 		}
-		
+
 		// 是否对超类进行注入
 		if (injectSuperClass) {
 			Class<?> c = targetClass.getSuperclass();
@@ -176,14 +176,14 @@ public class AopFactory {
 			}
 		}
 	}
-	
+
 	protected Object createObject(Class<?> targetClass) throws ReflectiveOperationException {
 		return Proxy.get(targetClass);
 	}
-	
+
 	/**
 	 * 字符串包含判断之 "_$$_" 支持 javassist,"$$Enhancer" 支持 cglib
-	 * 
+	 *
 	 * 被 cglib、guice 增强过的类需要通过本方法获取到被增强之前的类型
 	 * 否则调用其 targetClass.getDeclaredFields() 方法时
 	 * 获取到的是一堆 cglib guice 生成类中的 Field 对象
@@ -196,19 +196,19 @@ public class AopFactory {
 	    String n = clazz.getName();
 	    return (Class<?>)(n.indexOf("_$$_") > -1 || n.indexOf("$$Enhancer") > -1 ? clazz.getSuperclass() : clazz);
 	}
-	
+
 	/**
-	 * 设置被注入的对象是否为单例,可使用 @Singleton(boolean) 覆盖此默认值 
+	 * 设置被注入的对象是否为单例,可使用 @Singleton(boolean) 覆盖此默认值
 	 */
 	public AopFactory setSingleton(boolean singleton) {
 		this.singleton = singleton;
 		return this;
 	}
-	
+
 	public boolean isSingleton() {
 		return singleton;
 	}
-	
+
 	/**
 	 * 设置是否对超类进行注入
 	 */
@@ -216,11 +216,11 @@ public class AopFactory {
 		this.injectSuperClass = injectSuperClass;
 		return this;
 	}
-	
+
 	public boolean isInjectSuperClass() {
 		return injectSuperClass;
 	}
-	
+
 	public AopFactory addSingletonObject(Class<?> type, Object singletonObject) {
 		if (type == null) {
 			throw new IllegalArgumentException("type can not be null");
@@ -231,39 +231,39 @@ public class AopFactory {
 		if (singletonObject instanceof Class) {
 			throw new IllegalArgumentException("singletonObject can not be Class type");
 		}
-		
+
 		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());
 		}
-		
+
 		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");
 		}
-		
+
 		if (mapping == null) {
 			mapping = new HashMap<Class<?>, Class<?>>(128, 0.25F);
 		} else if (mapping.containsKey(from)) {
 			throw new RuntimeException("Class already mapped : " + from.getName());
 		}
-		
+
 		mapping.put(from, to);
 		return this;
 	}
-	
+
 	public <T> AopFactory addMapping(Class<T> from, String to) {
 		try {
 			@SuppressWarnings("unchecked")
@@ -277,10 +277,10 @@ public class AopFactory {
 			throw new RuntimeException(e);
 		}
 	}
-	
+
 	/**
 	 * 获取父类到子类的映射值,或者接口到实现类的映射值
-	 * @param from 父类或者接口 
+	 * @param from 父类或者接口
 	 * @return 如果映射存在则返回映射值,否则返回参数 from 的值
 	 */
 	public Class<?> getMappingClass(Class<?> from) {

+ 42 - 41
src/main/java/com/jfinal/aop/AopManager.java

@@ -22,66 +22,66 @@ import com.jfinal.core.Const;
  * AopManager
  */
 public class AopManager {
-	
+
 	private boolean injectDependency = Const.DEFAULT_INJECT_DEPENDENCY;
-	
+
 	private static final AopManager me = new AopManager();
-	
+
 	private AopManager() {}
-	
+
 	public static AopManager me() {
 		return me;
 	}
-	
+
 	/**
 	 * 设置对 Controller、Interceptor、Validator 进行依赖注入,默认值为 false
-	 * 
+	 *
 	 * 被注入对象默认为 singleton,可以通过 AopManager.me().setSingleton(boolean) 配置
 	 * 该默认值。
-	 * 
+	 *
 	 * 也可通过在被注入的目标类上使用 Singleton 注解覆盖上述默认值,注解配置
 	 * 优先级高于默认配置
-	 * 
+	 *
 	 * 注意:该配置仅针对于配置 jfinal web。而 Aop.get(...)、Aop.inject(...) 默认就会进行注入,
 	 *      无需配置
 	 */
 	public void setInjectDependency(boolean injectDependency) {
 		this.injectDependency = injectDependency;
 	}
-	
+
 	public boolean isInjectDependency() {
 		return injectDependency;
 	}
-	
+
 	/**
 	 * 设置是否对超类进行注入
 	 */
 	public void setInjectSuperClass(boolean injectSuperClass) {
 		Aop.aopFactory.setInjectSuperClass(injectSuperClass);
 	}
-	
+
 	public boolean isInjectSuperClass() {
 		return Aop.aopFactory.isInjectSuperClass();
 	}
-	
+
 	/**
 	 * 添加单例对象
-	 * 
+	 *
 	 * 由于 Aop 创建对象时不支持为构造方法传递参数,故添加此方法
-	 * 
+	 *
 	 * <pre>
 	 * 示例:
 	 * // Service 类的构造方法中传入了两个参数
 	 * Service service = new Service(paraAaa, paraBbb);
 	 * AopManager.me().addSingletonObject(Service.class, service);
-	 * 
-	 * // 上面代码添加完成以后,可以在任何地方通过下面的方式获取单例对象 
+	 *
+	 * // 上面代码添加完成以后,可以在任何地方通过下面的方式获取单例对象
 	 * service = Aop.get(Service.class);
-	 * 
+	 *
 	 * // 被添加进去的对象还可以用于注入
 	 * @Inject
 	 * Service service;
-	 * 
+	 *
 	 * // 在添加为单例对象之前还可以先为其注入依赖对象
 	 * Service service = new Service(paraAaa, paraBbb);
 	 * Aop.inject(service);		// 这里是对 Service 进行依赖注入
@@ -91,79 +91,79 @@ public class AopManager {
 	public void addSingletonObject(Class<?> type, Object singletonObject) {
 		Aop.aopFactory.addSingletonObject(type, singletonObject);
 	}
-	
+
 	public void addSingletonObject(Object singletonObject) {
 		Aop.aopFactory.addSingletonObject(singletonObject);
 	}
-	
+
 	/**
 	 * 添加父类到子类的映射,或者接口到实现类的映射。
-	 * 
+	 *
 	 * 该方法用于为父类、抽象类、或者接口注入子类或者实现类
-	 * 
+	 *
 	 * <pre>
 	 * 示例:
 	 * // 定义接口
 	 * public interface IService {
 	 *    public void justDoIt();
 	 * }
-	 * 
+	 *
 	 * // 定义接口的实现类
 	 * public class MyService implements IService {
 	 *   public void justDoIt() {
 	 *      ...
 	 *   }
 	 * }
-	 * 
+	 *
 	 * // 添加接口与实现类的映射关系
 	 * AopManager.me().addMapping(IService.class, MyService.class)
-	 * 
+	 *
 	 * public class MyController {
-	 * 
+	 *
 	 *    // 由于前面添加了接口与实现类的关系,所以下面将被注入实现类 MyService 对象
 	 *    @Inject
 	 *    IService service
-	 *    
+	 *
 	 *    public action() {
 	 *       service.justDoIt();
 	 *    }
 	 * }
-	 * 
+	 *
 	 * 如上所示,通过建立了 IService 与 MyService 的映射关系,在 @Inject 注入的时候
 	 * 就会注入映射好的实现类,当然也可以通过在 @Inject 注解中指定实现类来实现:
-	 * 
+	 *
 	 * @Inject(MyService.class)
 	 * IService service
-	 * 
+	 *
 	 * 但是上面的的方法是写死在代码中的,不方便改变实现类
-	 * 
+	 *
 	 * </pre>
-	 * 
+	 *
 	 * @param from 父类或者接口
 	 * @param to 父类的子类或者接口的实现类
 	 */
 	public <T> void addMapping(Class<T> from, Class<? extends T> to) {
 		Aop.aopFactory.addMapping(from, to);
 	}
-	
+
 	/**
 	 * 功能与 addMapping(Class<T> from, Class<? extends T> to) 相同,仅仅是第二个参数
 	 * 由 Class 改为 String 类型,便于从外部配置文件传递 String 参数过来
-	 * 
+	 *
 	 * <pre>
 	 * 示例:
 	 * AopManager.me().addMapping(IService.class, "com.xxx.MyService")
-	 * 
+	 *
 	 * 以上代码的参数 "com.xxx.MyService" 可通过外部配置文件传入,便于通过配置文件切换接口的
 	 * 实现类:
 	 * AopManager.me().addMapping(IService.class, PropKit.get("ServiceImpl"));
-	 * 
+	 *
 	 * </pre>
 	 */
 	public <T> void addMapping(Class<T> from, String to) {
 		Aop.aopFactory.addMapping(from, to);
 	}
-	
+
 	/**
 	 * 设置 AopFactory,便于扩展自己的 AopFactory 实现
 	 */
@@ -173,19 +173,20 @@ public class AopManager {
 		}
 		Aop.aopFactory = aopFactory;
 	}
-	
+
 	public AopFactory getAopFactory() {
 		return Aop.aopFactory;
 	}
-	
+
 	/**
-	 * 设置被注入的对象是否为单例,可在目标类上使用 @Singleton(boolean) 覆盖此默认值 
+	 * 设置被注入的对象是否为单例,可在目标类上使用 @Singleton(boolean) 覆盖此默认值
 	 */
 	public void setSingleton(boolean singleton) {
 		Aop.aopFactory.setSingleton(singleton);
 	}
-	
+
 	public boolean isSingleton() {
 		return Aop.aopFactory.isSingleton();
 	}
 }
+