diff --git a/src/main/java/org/redkale/annotation/AutoLoad.java b/src/main/java/org/redkale/annotation/AutoLoad.java index 0ee137df3..faa96cd94 100644 --- a/src/main/java/org/redkale/annotation/AutoLoad.java +++ b/src/main/java/org/redkale/annotation/AutoLoad.java @@ -5,13 +5,14 @@ */ package org.redkale.annotation; +import java.lang.annotation.*; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; -import java.lang.annotation.*; - /** - * 自动加载。 使用场景: 1、被标记为@AutoLoad(false)的Service类不会被自动加载, 当被依赖时才会被加载 2、被标记为@AutoLoad(false)的Servlet类不会被自动加载 + * 自动加载。 使用场景:
+ * 1、被标记为@AutoLoad(false)的Service类不会被自动加载, 当被依赖时才会被加载
+ * 2、被标记为@AutoLoad(false)的Servlet类不会被自动加载
* *

详情见: https://redkale.org * diff --git a/src/main/java/org/redkale/inject/ResourceAnnotationLoader.java b/src/main/java/org/redkale/inject/ResourceAnnotationLoader.java index 522d3f48a..f770c0bbf 100644 --- a/src/main/java/org/redkale/inject/ResourceAnnotationLoader.java +++ b/src/main/java/org/redkale/inject/ResourceAnnotationLoader.java @@ -9,6 +9,52 @@ import java.lang.reflect.Field; /** * 自定义注入加载器 * + *

+ *
+ *
+ * @Documented
+ * @Target({FIELD})
+ * @Retention(RUNTIME)
+ * public @interface CustomConf {
+ *     String path();
+ * }
+ *
+ *
+ *  public class CustomConfProvider implements ResourceAnnotationLoader<CustomConf> {
+ *
+ *      @Override
+ *      public void load(
+ *              ResourceFactory factory,
+ *              String srcResourceName,
+ *              Object srcObj,
+ *              CustomConf annotation,
+ *              Field field,
+ *              Object attachment) {
+ *          try {
+ *              field.set(srcObj, new File(annotation.path()));
+ *          } catch (Exception e) {
+ *              e.printStackTrace();
+ *          }
+ *          System.out.println("对象是 src =" + srcObj + ", path=" + annotation.path());
+ *      }
+ *
+ *      @Override
+ *      public Class<CustomConf> annotationType() {
+ *          return CustomConf.class;
+ *      }
+ *  }
+ *
+ *
+ *  public class InjectBean {
+ *
+ *      @CustomConf(path = "conf/test.xml")
+ *      public File conf;
+ *  }
+ *
+ *
+ * 
+ *
+ * *

详情见: https://redkale.org * * @since 2.8.0 diff --git a/src/main/java/org/redkale/inject/ResourceFactory.java b/src/main/java/org/redkale/inject/ResourceFactory.java index 3b467358c..ffc4f060b 100644 --- a/src/main/java/org/redkale/inject/ResourceFactory.java +++ b/src/main/java/org/redkale/inject/ResourceFactory.java @@ -106,17 +106,23 @@ public final class ResourceFactory { return result; } - /** 清空当前已注入资源的缓存 */ + /** + * 清空当前已注入资源的缓存 + */ public void release() { this.entryStore.clear(); } - /** inject时的锁 */ + /** + * inject时的锁 + */ public void lock() { lock.lock(); } - /** inject时的锁 */ + /** + * inject时的锁 + */ public void unlock() { lock.unlock(); } @@ -279,10 +285,7 @@ public final class ResourceFactory { * @return 旧资源对象 */ public A register(final boolean autoSync, final A rs) { - if (rs == null) { - return null; - } - return register(autoSync, "", rs); + return rs == null ? null : register(autoSync, "", rs); } /** @@ -623,6 +626,7 @@ public final class ResourceFactory { * 注册Configuration配置类 * * @param configuareClass 标记Configuration的类 + * @return 方法数 * */ public int registerConfiguration(final Class configuareClass) { @@ -871,20 +875,62 @@ public final class ResourceFactory { return parent == null ? null : parent.findSuperTypeLoader(ft, field); } + /** + * 找指定类型对应的资源对象 + * + * @param 泛型 + * @param clazz 资源类型 + * @return 资源对象 + */ public A find(Class clazz) { return find("", clazz); } - public A find(String name, Type clazz) { - ResourceEntry re = findEntry(name, clazz); - return re == null ? null : (A) re.value; - } - + /** + * 找指定类型和资源名对应的资源对象 + * + * @param 泛型 + * @param name 资源名 + * @param clazz 资源类型 + * @return 资源对象 + */ public A find(String name, Class clazz) { ResourceEntry re = findEntry(name, clazz); return re == null ? null : re.value; } + /** + * 找指定类型对应的资源对象 + * + * @param 泛型 + * @param clazz 资源类型 + * @return 资源对象 + */ + public A find(Type clazz) { + return find("", clazz); + } + + /** + * 找指定类型和资源名对应的资源对象 + * + * @param 泛型 + * @param name 资源名 + * @param clazz 资源类型 + * @return 资源对象 + */ + public A find(String name, Type clazz) { + ResourceEntry re = findEntry(name, clazz); + return re == null ? null : (A) re.value; + } + + /** + * 找指定类型或子类型和资源名对应的资源对象 + * + * @param 泛型 + * @param name 资源名 + * @param clazz 资源类型 + * @return 资源对象 + */ public A findChild(final String name, final Class clazz) { A rs = find(name, clazz); if (rs != null) { @@ -905,6 +951,13 @@ public final class ResourceFactory { return null; } + /** + * 找指定类型和资源名对应的资源对象 + * @param 泛型 + * @param name 资源名 + * @param clazz 资源类型 + * @return 资源对象 + */ private ResourceEntry findEntry(String name, Type clazz) { Map map = this.entryStore.get(clazz); if (map != null) { @@ -919,6 +972,33 @@ public final class ResourceFactory { return null; } + /** + * 加载资源对象, 没有返回null + * + * @param 泛型 + * @param clazz 资源类型 + * @return 资源对象 + * + * @since 2.8.0 + */ + public A load(Class clazz) { + return load("", (Type) clazz); + } + + /** + * 加载资源对象, 没有返回null + * + * @param 泛型 + * @param name 资源名 + * @param clazz 资源类型 + * @return 资源对象 + * + * @since 2.8.0 + */ + public A load(String name, Class clazz) { + return load(name, (Type) clazz); + } + /** * 加载资源对象, 没有返回null * @@ -962,14 +1042,36 @@ public final class ResourceFactory { return val; } + /** + * 获取指定类型的资源对象 + * + * @param 泛型 + * @param clazz 资源类型 + * @return 资源集合 + */ public List query(Class clazz) { return query(new ArrayList<>(), clazz); } + /** + * 获取指定类型的资源对象 + * + * @param 泛型 + * @param clazz 资源类型 + * @return 资源集合 + */ public List query(Type clazz) { return query(new ArrayList<>(), clazz); } + /** + * 获取指定类型的资源对象 + * + * @param 泛型 + * @param list 资源集合 + * @param clazz 资源类型 + * @return 资源集合 + */ private List query(final List list, Type clazz) { Map map = this.entryStore.get(clazz); if (map != null) { @@ -985,10 +1087,25 @@ public final class ResourceFactory { return list; } + /** + * 获取符合过滤条件的资源对象 + * + * @param 泛型 + * @param predicate 资源过滤条件 + * @return 资源集合 + */ public List query(final BiPredicate predicate) { return query(new ArrayList<>(), predicate); } + /** + * 获取符合过滤条件的资源对象 + * + * @param 泛型 + * @param list 资源集合 + * @param predicate 资源过滤条件 + * @return 资源集合 + */ private List query(final List list, final BiPredicate predicate) { if (predicate == null) { return list; @@ -1452,11 +1569,11 @@ public final class ResourceFactory { this.name = name; this.value = value; this.elements = elements == null ? new CopyOnWriteArrayList<>() : elements; - if (sync && Utility.isNotEmpty(elements)) { + if (sync && elements != null && !elements.isEmpty()) { for (ResourceElement element : elements) { Object dest = element.dest.get(); - if (dest == null) { - continue; // 依赖对象可能被销毁了 + if (dest == null) { // 依赖对象可能被销毁了 + continue; } Object newVal = Utility.convertValue(element.fieldType, value); Object oldVal = null; diff --git a/src/main/java/org/redkale/inject/ResourceTypeLoader.java b/src/main/java/org/redkale/inject/ResourceTypeLoader.java index 97d8a22e2..4f8577abc 100644 --- a/src/main/java/org/redkale/inject/ResourceTypeLoader.java +++ b/src/main/java/org/redkale/inject/ResourceTypeLoader.java @@ -9,6 +9,54 @@ import org.redkale.annotation.Nullable; /** * 自定义注入加载器 * + *

+ *
+ *
+ *  public class CustomConfProvider implements ResourceAnnotationLoader<CustomConf> {
+ *
+ *      @Override
+ *      public Object load(
+ *              ResourceFactory factory,
+ *              String srcResourceName,
+ *              Object srcObj,
+ *              CustomConf annotation,
+ *              Field field,
+ *              Object attachment) {
+ *          DataSource source = new DataMemorySource(resourceName);
+ *          factory.register(resourceName, DataSource.class, source);
+ *          if (field != null) {
+ *              try {
+ *                  field.set(srcObj, source);
+ *              } catch (Exception e) {
+ *                  e.printStackTrace();
+ *              }
+ *          }
+ *          return source;
+ *      }
+ *
+ *      @Override
+ *      public Type resourceType() {
+ *          return DataSource.class;
+ *      }
+ *  }
+ *
+ *
+ *  public class InjectBean {
+ *
+ *      @Resource(name = "platf")
+ *      public DataSource source;
+ *  }
+ *
+ *
+ *  ResourceFactory factory = ResourceFactory.create();
+ *  factory.register(new DataSourceProvider());
+ *  InjectBean bean = new InjectBean();
+ *  factory.inject(bean);
+ *
+ *
+ * 
+ *
+ * *

详情见: https://redkale.org * * @author zhangjx diff --git a/src/test/java/org/redkale/test/inject/ResourceTypeTest.java b/src/test/java/org/redkale/test/inject/ResourceTypeTest.java new file mode 100644 index 000000000..33280dec4 --- /dev/null +++ b/src/test/java/org/redkale/test/inject/ResourceTypeTest.java @@ -0,0 +1,68 @@ +/* + +*/ + +package org.redkale.test.inject; + +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import org.junit.jupiter.api.Test; +import org.redkale.annotation.Resource; +import org.redkale.inject.ResourceFactory; +import org.redkale.inject.ResourceTypeLoader; +import org.redkale.source.DataMemorySource; +import org.redkale.source.DataSource; + +/** + * + * @author zhangjx + */ +public class ResourceTypeTest { + + public static void main(String[] args) throws Throwable { + ResourceTypeTest test = new ResourceTypeTest(); + test.run(); + } + + @Test + public void run() throws Exception { + ResourceFactory factory = ResourceFactory.create(); + factory.register(new DataSourceProvider()); + InjectBean bean = new InjectBean(); + factory.inject(bean); + } + + public static class DataSourceProvider implements ResourceTypeLoader { + + @Override + public Object load( + ResourceFactory factory, + String srcResourceName, + Object srcObj, + String resourceName, + Field field, + Object attachment) { + DataSource source = new DataMemorySource(resourceName); + factory.register(resourceName, DataSource.class, source); + if (field != null) { + try { + field.set(srcObj, source); + } catch (Exception e) { + e.printStackTrace(); + } + } + return source; + } + + @Override + public Type resourceType() { + return DataSource.class; + } + } + + public static class InjectBean { + + @Resource(name = "platf") + public DataSource source; + } +}