注释优化

This commit is contained in:
redkale
2024-08-08 23:08:35 +08:00
parent aa47820ee7
commit 33c579f14f
5 changed files with 298 additions and 18 deletions

View File

@@ -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类不会被自动加载
* 自动加载。 使用场景: <br>
* 1、被标记为&#64;AutoLoad(false)的Service类不会被自动加载, 当被依赖时才会被加载 <br>
* 2、被标记为&#64;AutoLoad(false)的Servlet类不会被自动加载 <br>
*
* <p>详情见: https://redkale.org
*

View File

@@ -9,6 +9,52 @@ import java.lang.reflect.Field;
/**
* 自定义注入加载器
*
* <blockquote>
* <pre>
*
* &#064;Documented
* &#064;Target({FIELD})
* &#064;Retention(RUNTIME)
* public @interface CustomConf {
* String path();
* }
*
*
* public class CustomConfProvider implements ResourceAnnotationLoader&lt;CustomConf&gt; {
*
* &#064;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());
* }
*
* &#064;Override
* public Class&lt;CustomConf&gt; annotationType() {
* return CustomConf.class;
* }
* }
*
*
* public class InjectBean {
*
* &#064;CustomConf(path = "conf/test.xml")
* public File conf;
* }
*
*
* </pre>
* </blockquote>
*
* <p>详情见: https://redkale.org
*
* @since 2.8.0

View File

@@ -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> 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 <A> 泛型
* @param clazz 资源类型
* @return 资源对象
*/
public <A> A find(Class<? extends A> clazz) {
return find("", clazz);
}
public <A> A find(String name, Type clazz) {
ResourceEntry re = findEntry(name, clazz);
return re == null ? null : (A) re.value;
}
/**
* 找指定类型和资源名对应的资源对象
*
* @param <A> 泛型
* @param name 资源名
* @param clazz 资源类型
* @return 资源对象
*/
public <A> A find(String name, Class<? extends A> clazz) {
ResourceEntry<A> re = findEntry(name, clazz);
return re == null ? null : re.value;
}
/**
* 找指定类型对应的资源对象
*
* @param <A> 泛型
* @param clazz 资源类型
* @return 资源对象
*/
public <A> A find(Type clazz) {
return find("", clazz);
}
/**
* 找指定类型和资源名对应的资源对象
*
* @param <A> 泛型
* @param name 资源名
* @param clazz 资源类型
* @return 资源对象
*/
public <A> A find(String name, Type clazz) {
ResourceEntry re = findEntry(name, clazz);
return re == null ? null : (A) re.value;
}
/**
* 找指定类型或子类型和资源名对应的资源对象
*
* @param <A> 泛型
* @param name 资源名
* @param clazz 资源类型
* @return 资源对象
*/
public <A> A findChild(final String name, final Class<? extends A> clazz) {
A rs = find(name, clazz);
if (rs != null) {
@@ -905,6 +951,13 @@ public final class ResourceFactory {
return null;
}
/**
* 找指定类型和资源名对应的资源对象
* @param <A> 泛型
* @param name 资源名
* @param clazz 资源类型
* @return 资源对象
*/
private <A> ResourceEntry<A> findEntry(String name, Type clazz) {
Map<String, ResourceEntry> map = this.entryStore.get(clazz);
if (map != null) {
@@ -919,6 +972,33 @@ public final class ResourceFactory {
return null;
}
/**
* 加载资源对象, 没有返回null
*
* @param <A> 泛型
* @param clazz 资源类型
* @return 资源对象
*
* @since 2.8.0
*/
public <A> A load(Class<A> clazz) {
return load("", (Type) clazz);
}
/**
* 加载资源对象, 没有返回null
*
* @param <A> 泛型
* @param name 资源名
* @param clazz 资源类型
* @return 资源对象
*
* @since 2.8.0
*/
public <A> A load(String name, Class<A> clazz) {
return load(name, (Type) clazz);
}
/**
* 加载资源对象, 没有返回null
*
@@ -962,14 +1042,36 @@ public final class ResourceFactory {
return val;
}
/**
* 获取指定类型的资源对象
*
* @param <A> 泛型
* @param clazz 资源类型
* @return 资源集合
*/
public <A> List<A> query(Class<? extends A> clazz) {
return query(new ArrayList<>(), clazz);
}
/**
* 获取指定类型的资源对象
*
* @param <A> 泛型
* @param clazz 资源类型
* @return 资源集合
*/
public <A> List<A> query(Type clazz) {
return query(new ArrayList<>(), clazz);
}
/**
* 获取指定类型的资源对象
*
* @param <A> 泛型
* @param list 资源集合
* @param clazz 资源类型
* @return 资源集合
*/
private <A> List<A> query(final List<A> list, Type clazz) {
Map<String, ResourceEntry> map = this.entryStore.get(clazz);
if (map != null) {
@@ -985,10 +1087,25 @@ public final class ResourceFactory {
return list;
}
/**
* 获取符合过滤条件的资源对象
*
* @param <A> 泛型
* @param predicate 资源过滤条件
* @return 资源集合
*/
public <A> List<A> query(final BiPredicate<String, Object> predicate) {
return query(new ArrayList<>(), predicate);
}
/**
* 获取符合过滤条件的资源对象
*
* @param <A> 泛型
* @param list 资源集合
* @param predicate 资源过滤条件
* @return 资源集合
*/
private <A> List<A> query(final List<A> list, final BiPredicate<String, Object> 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;

View File

@@ -9,6 +9,54 @@ import org.redkale.annotation.Nullable;
/**
* 自定义注入加载器
*
* <blockquote>
* <pre>
*
* public class CustomConfProvider implements ResourceAnnotationLoader&lt;CustomConf&gt; {
*
* &#064;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;
* }
*
* &#064;Override
* public Type resourceType() {
* return DataSource.class;
* }
* }
*
*
* public class InjectBean {
*
* &#064;Resource(name = "platf")
* public DataSource source;
* }
*
*
* ResourceFactory factory = ResourceFactory.create();
* factory.register(new DataSourceProvider());
* InjectBean bean = new InjectBean();
* factory.inject(bean);
*
*
* </pre>
* </blockquote>
*
* <p>详情见: https://redkale.org
*
* @author zhangjx

View File

@@ -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;
}
}