ResourceTypeLoader优化
This commit is contained in:
@@ -34,6 +34,7 @@ import org.redkale.convert.Convert;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.convert.proto.ProtobufFactory;
|
||||
import org.redkale.inject.ResourceAnnotationLoader;
|
||||
import org.redkale.inject.ResourceEvent;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.inject.ResourceTypeLoader;
|
||||
@@ -51,6 +52,7 @@ import org.redkale.source.*;
|
||||
import org.redkale.source.spi.SourceModuleEngine;
|
||||
import org.redkale.util.AnyValue;
|
||||
import org.redkale.util.AnyValueWriter;
|
||||
import org.redkale.util.Creator;
|
||||
import org.redkale.util.Environment;
|
||||
import org.redkale.util.Redkale;
|
||||
import org.redkale.util.RedkaleClassLoader;
|
||||
@@ -702,23 +704,67 @@ public final class Application {
|
||||
}
|
||||
});
|
||||
// 加载Configuration
|
||||
ClassFilter<?> filter = new ClassFilter(this.getClassLoader(), Configuration.class, Object.class);
|
||||
ClassFilter<?> resConfigFilter = new ClassFilter(this.getClassLoader(), Configuration.class, Object.class);
|
||||
ClassFilter<ResourceAnnotationLoader> resAnnFilter =
|
||||
new ClassFilter(this.getClassLoader(), ResourceAnnotationLoader.class);
|
||||
ClassFilter<ResourceTypeLoader> resTypeFilter =
|
||||
new ClassFilter(this.getClassLoader(), ResourceTypeLoader.class);
|
||||
try {
|
||||
loadClassByFilters(filter);
|
||||
loadClassByFilters(resConfigFilter, resAnnFilter, resTypeFilter);
|
||||
} catch (IOException e) {
|
||||
throw new RedkaleException(e);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
filter.getFilterEntrys().forEach(en -> {
|
||||
int c = resourceFactory.registerConfiguration(en.getType());
|
||||
sb.append("Load Configuration (type=")
|
||||
.append(en.getType().getName())
|
||||
.append(") ")
|
||||
.append(c)
|
||||
.append(" resources\r\n");
|
||||
});
|
||||
if (sb.length() > 0) {
|
||||
logger.log(Level.INFO, sb.toString().trim());
|
||||
{ // Configuration
|
||||
StringBuilder sb = new StringBuilder();
|
||||
resConfigFilter.getFilterEntrys().forEach(en -> {
|
||||
AutoLoad auto = en.getType().getAnnotation(AutoLoad.class);
|
||||
if (auto == null || auto.value()) {
|
||||
int c = resourceFactory.registerConfiguration(en.getType());
|
||||
sb.append("Load Configuration (type=")
|
||||
.append(en.getType().getName())
|
||||
.append(") ")
|
||||
.append(c)
|
||||
.append(" resources\r\n");
|
||||
}
|
||||
});
|
||||
if (sb.length() > 0) {
|
||||
logger.log(Level.INFO, sb.toString().trim());
|
||||
}
|
||||
}
|
||||
{ // ResourceAnnotationLoader
|
||||
StringBuilder sb = new StringBuilder();
|
||||
resAnnFilter.getFilterEntrys().forEach(en -> {
|
||||
AutoLoad auto = en.getType().getAnnotation(AutoLoad.class);
|
||||
if (auto == null || auto.value()) {
|
||||
ResourceAnnotationLoader loader =
|
||||
Creator.create(en.getType()).create();
|
||||
resourceFactory.register(loader);
|
||||
sb.append("Load ResourceAnnotationLoader (type=")
|
||||
.append(en.getType().getName())
|
||||
.append(", annotation=")
|
||||
.append(loader.annotationType().getName())
|
||||
.append(")\r\n");
|
||||
}
|
||||
});
|
||||
if (sb.length() > 0) {
|
||||
logger.log(Level.INFO, sb.toString().trim());
|
||||
}
|
||||
}
|
||||
{ // ResourceTypeLoader
|
||||
StringBuilder sb = new StringBuilder();
|
||||
resTypeFilter.getFilterEntrys().forEach(en -> {
|
||||
AutoLoad auto = en.getType().getAnnotation(AutoLoad.class);
|
||||
if (auto == null || auto.value()) {
|
||||
ResourceTypeLoader loader = Creator.create(en.getType()).create();
|
||||
resourceFactory.register(loader);
|
||||
sb.append("Load ResourceTypeLoader (type=")
|
||||
.append(en.getType().getName())
|
||||
.append(")\r\n");
|
||||
}
|
||||
});
|
||||
if (sb.length() > 0) {
|
||||
logger.log(Level.INFO, sb.toString().trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.redkale.util.Utility;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class NodeAutoServiceLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.lang.reflect.Type;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.annotation.Priority;
|
||||
import org.redkale.asm.AsmMethodBoost;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
@@ -25,6 +26,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class NodeExpectServiceLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.net.SocketAddress;
|
||||
import java.util.HashSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.asm.AsmMethodBoost;
|
||||
import static org.redkale.boot.Application.RESNAME_SNCP_ADDRESS;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
@@ -29,6 +30,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class NodeWebSocketNodeLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.ServiceLoader;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.inject.ResourceTypeLoader;
|
||||
import org.redkale.service.Service;
|
||||
@@ -21,6 +22,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class CachedKeyGeneratorLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.cached.CachedManager;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.inject.ResourceTypeLoader;
|
||||
@@ -20,6 +21,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class CachedManagerLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.lang.reflect.Field;
|
||||
* }
|
||||
*
|
||||
*
|
||||
* public class CustomConfProvider implements ResourceAnnotationLoader<CustomConf> {
|
||||
* public class CustomConfAnnotationLoader implements ResourceAnnotationLoader<CustomConf> {
|
||||
*
|
||||
* @Override
|
||||
* public void load(
|
||||
@@ -52,6 +52,12 @@ import java.lang.reflect.Field;
|
||||
* }
|
||||
*
|
||||
*
|
||||
* ResourceFactory factory = ResourceFactory.create();
|
||||
* factory.register(new CustomConfAnnotationLoader());
|
||||
* InjectBean bean = new InjectBean();
|
||||
* factory.inject(bean);
|
||||
*
|
||||
*
|
||||
* </pre>
|
||||
* </blockquote>
|
||||
*
|
||||
|
||||
@@ -610,7 +610,8 @@ public final class ResourceFactory {
|
||||
*/
|
||||
public <T extends Annotation> void register(final ResourceAnnotationLoader<T> loader) {
|
||||
Objects.requireNonNull(loader);
|
||||
parentRoot().resAnnotationLoaderMap.put(loader.annotationType(), loader);
|
||||
inject(loader);
|
||||
resAnnotationLoaderMap.put(loader.annotationType(), loader);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,6 +620,8 @@ public final class ResourceFactory {
|
||||
* @param loader ResourceTypeLoader
|
||||
*/
|
||||
public void register(final ResourceTypeLoader loader) {
|
||||
Objects.requireNonNull(loader);
|
||||
inject(loader);
|
||||
resTypeLoaderMap.put(loader.resourceType(), loader);
|
||||
}
|
||||
|
||||
@@ -632,6 +635,7 @@ public final class ResourceFactory {
|
||||
public int registerConfiguration(final Class configuareClass) {
|
||||
int count = 0;
|
||||
Object instance = Creator.create(configuareClass).create();
|
||||
inject(instance);
|
||||
for (Method method : configuareClass.getDeclaredMethods()) {
|
||||
Resource res = method.getAnnotation(Resource.class);
|
||||
if (res == null) {
|
||||
@@ -803,6 +807,21 @@ public final class ResourceFactory {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型对应的ResourceAnnotationLoader <br>
|
||||
*
|
||||
* @param <A> 泛型
|
||||
* @param clazz 类型
|
||||
* @return ResourceAnnotationLoader
|
||||
*/
|
||||
public <A extends Annotation> ResourceAnnotationLoader<A> findResourceAnnotationLoader(Class<A> clazz) {
|
||||
ResourceAnnotationLoader<A> it = this.resAnnotationLoaderMap.get(clazz);
|
||||
if (it != null) {
|
||||
return it;
|
||||
}
|
||||
return parent == null ? null : parent.findResourceAnnotationLoader(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型对应的ResourceTypeLoader <br>
|
||||
*
|
||||
@@ -1246,7 +1265,6 @@ public final class ResourceFactory {
|
||||
try {
|
||||
list.add(srcObj);
|
||||
Class clazz = srcObj.getClass();
|
||||
final boolean diyLoaderFlag = !parentRoot().resAnnotationLoaderMap.isEmpty();
|
||||
do {
|
||||
if (java.lang.Enum.class.isAssignableFrom(clazz)) {
|
||||
break;
|
||||
@@ -1287,14 +1305,13 @@ public final class ResourceFactory {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag && diyLoaderFlag) {
|
||||
parentRoot().resAnnotationLoaderMap.values().stream()
|
||||
.forEach(iloader -> {
|
||||
Annotation ann = field.getAnnotation(iloader.annotationType());
|
||||
if (ann != null) {
|
||||
iloader.load(this, srcResourceName, srcObj, ann, field, attachment);
|
||||
}
|
||||
});
|
||||
if (flag) {
|
||||
for (Annotation ann : field.getAnnotations()) {
|
||||
ResourceAnnotationLoader iloader = findResourceAnnotationLoader(ann.annotationType());
|
||||
if (iloader != null) {
|
||||
iloader.load(this, srcResourceName, srcObj, ann, field, attachment);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ns == null) {
|
||||
continue;
|
||||
@@ -1488,11 +1505,11 @@ public final class ResourceFactory {
|
||||
*
|
||||
* @return ResourceFactory
|
||||
*/
|
||||
private ResourceFactory parentRoot() {
|
||||
private ResourceFactory parentRoot2() {
|
||||
if (parent == null) {
|
||||
return this;
|
||||
}
|
||||
return parent.parentRoot();
|
||||
return parent.parentRoot2();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.redkale.annotation.Nullable;
|
||||
* <blockquote>
|
||||
* <pre>
|
||||
*
|
||||
* public class CustomConfProvider implements ResourceAnnotationLoader<CustomConf> {
|
||||
* public class CustomTypeLoader implements ResourceTypeLoader {
|
||||
*
|
||||
* @Override
|
||||
* public Object load(
|
||||
@@ -49,7 +49,7 @@ import org.redkale.annotation.Nullable;
|
||||
*
|
||||
*
|
||||
* ResourceFactory factory = ResourceFactory.create();
|
||||
* factory.register(new DataSourceProvider());
|
||||
* factory.register(new CustomTypeLoader());
|
||||
* InjectBean bean = new InjectBean();
|
||||
* factory.inject(bean);
|
||||
*
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.annotation.Resource;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.inject.ResourceTypeLoader;
|
||||
@@ -21,6 +22,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class CacheSourceLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.inject.ResourceTypeLoader;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
@@ -19,6 +20,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class DataSourceLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.inject.ResourceTypeLoader;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
@@ -21,6 +22,7 @@ import org.redkale.util.RedkaleException;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
@AutoLoad(false)
|
||||
class DataSqlMapperLoader implements ResourceTypeLoader {
|
||||
|
||||
private final Logger logger = Logger.getLogger(getClass().getSimpleName());
|
||||
|
||||
@@ -5,11 +5,10 @@
|
||||
*/
|
||||
package org.redkale.test.inject;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.reflect.Field;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
@@ -19,11 +18,8 @@ import org.redkale.inject.ResourceFactory;
|
||||
/** @author zhangjx */
|
||||
public class ResourceAnnotationTest {
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
ResourceAnnotationTest test = new ResourceAnnotationTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@@ -33,7 +29,8 @@ public class ResourceAnnotationTest {
|
||||
factory.register(new CustomConfProvider());
|
||||
InjectBean bean = new InjectBean();
|
||||
factory.inject(bean);
|
||||
if (!main) Assertions.assertEquals(new File("conf/test.xml").toString(), bean.conf.toString());
|
||||
System.out.println(bean.conf);
|
||||
Assertions.assertEquals(new File("conf/test.xml").toString(), bean.conf.toString());
|
||||
}
|
||||
|
||||
public static class CustomConfProvider implements ResourceAnnotationLoader<CustomConf> {
|
||||
|
||||
Reference in New Issue
Block a user