diff --git a/src/org/redkale/boot/Application.java b/src/org/redkale/boot/Application.java index b1bdf02c9..035243a00 100644 --- a/src/org/redkale/boot/Application.java +++ b/src/org/redkale/boot/Application.java @@ -360,21 +360,30 @@ public final class Application { this.resourceFactory.register(JsonFactory.root().getConvert()); //只有WatchService才能加载Application、WatchFactory final Application application = this; - this.resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> { - try { - Resource res = field.getAnnotation(Resource.class); - if (res == null || !res.name().isEmpty()) return false; - if (!(src instanceof WatchService) || Sncp.isRemote((Service) src)) return false; //远程模式不得注入 - Class type = field.getType(); - if (type == Application.class) { - field.set(src, application); + this.resourceFactory.register(new ResourceFactory.ResourceLoader() { + + @Override + public void load(ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) { + try { + Resource res = field.getAnnotation(Resource.class); + if (res == null || !res.name().isEmpty()) return; + if (!(src instanceof WatchService) || Sncp.isRemote((Service) src)) return; //远程模式不得注入 + Class type = field.getType(); + if (type == Application.class) { + field.set(src, application); // } else if (type == WatchFactory.class) { // field.set(src, application.watchFactory); + } + } catch (Exception e) { + logger.log(Level.SEVERE, "Resource inject error", e); } - } catch (Exception e) { - logger.log(Level.SEVERE, "Resource inject error", e); } - return false; + + @Override + public boolean autoNone() { + return false; + } + }, Application.class, WatchFactory.class); //-------------------------------------------------------------------------- initResources(); diff --git a/src/org/redkale/boot/NodeHttpServer.java b/src/org/redkale/boot/NodeHttpServer.java index 4e6f1c77b..0a0d10ee0 100644 --- a/src/org/redkale/boot/NodeHttpServer.java +++ b/src/org/redkale/boot/NodeHttpServer.java @@ -92,8 +92,8 @@ public class NodeHttpServer extends NodeServer { final ResourceFactory regFactory = application.getResourceFactory(); resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务 try { - if (field.getAnnotation(Resource.class) == null) return true; - if (!(src instanceof WebSocketServlet)) return true; + if (field.getAnnotation(Resource.class) == null) return; + if (!(src instanceof WebSocketServlet)) return; synchronized (regFactory) { Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class); if (nodeService == null) { @@ -107,7 +107,6 @@ public class NodeHttpServer extends NodeServer { } catch (Exception e) { logger.log(Level.SEVERE, "WebSocketNode inject error", e); } - return true; }, WebSocketNode.class); } diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index 3a9082086..dba8b1f71 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -136,7 +136,7 @@ public abstract class NodeServer { if (this.sncpAddress != null) { this.resourceFactory.register(RESNAME_SNCP_ADDR, this.sncpAddress); this.resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, this.sncpAddress); - this.resourceFactory.register(RESNAME_SNCP_ADDR, String.class, this.sncpAddress.getHostString()+":" + this.sncpAddress.getPort()); + this.resourceFactory.register(RESNAME_SNCP_ADDR, String.class, this.sncpAddress.getHostString() + ":" + this.sncpAddress.getPort()); } if (this.sncpGroup != null) this.resourceFactory.register(RESNAME_SNCP_GROUP, this.sncpGroup); { @@ -215,10 +215,10 @@ public abstract class NodeServer { resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> { try { Resource res = field.getAnnotation(Resource.class); - if (res == null || !res.name().startsWith("properties.")) return true; - if ((src instanceof Service) && Sncp.isRemote((Service) src)) return true; //远程模式不得注入 DataSource + if (res == null || !res.name().startsWith("properties.")) return; + if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 DataSource Class type = field.getType(); - if (type != AnyValue.class && type != AnyValue[].class) return true; + if (type != AnyValue.class && type != AnyValue[].class) return; Object resource = null; final AnyValue properties = resources == null ? null : resources.getAnyValue("properties"); if (properties != null && type == AnyValue.class) { @@ -232,14 +232,13 @@ public abstract class NodeServer { } catch (Exception e) { logger.log(Level.SEVERE, "Resource inject error", e); } - return true; }, AnyValue.class, AnyValue[].class); //------------------------------------- 注册DataSource -------------------------------------------------------- resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> { try { - if (field.getAnnotation(Resource.class) == null) return true; - if ((src instanceof Service) && Sncp.isRemote((Service) src)) return true; //远程模式不得注入 DataSource + if (field.getAnnotation(Resource.class) == null) return; + if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 DataSource DataSource source = DataSources.createDataSource(resourceName); application.dataSources.add(source); appResFactory.register(resourceName, DataSource.class, source); @@ -265,14 +264,13 @@ public abstract class NodeServer { } catch (Exception e) { logger.log(Level.SEVERE, "DataSource inject error", e); } - return true; }, DataSource.class); //------------------------------------- 注册CacheSource -------------------------------------------------------- resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> { try { - if (field.getAnnotation(Resource.class) == null) return true; - if ((src instanceof Service) && Sncp.isRemote((Service) src)) return true; //远程模式不需要注入 CacheSource + if (field.getAnnotation(Resource.class) == null) return; + if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不需要注入 CacheSource final Service srcService = (Service) src; SncpClient client = Sncp.getSncpClient(srcService); Transport sameGroupTransport = Sncp.getSameGroupTransport(srcService); @@ -308,7 +306,6 @@ public abstract class NodeServer { } catch (Exception e) { logger.log(Level.SEVERE, "DataSource inject error", e); } - return true; }, CacheSource.class); } @@ -379,7 +376,6 @@ public abstract class NodeServer { if (entry.isExpect()) { ResourceFactory.ResourceLoader resourceLoader = (ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> { runner.accept(rf, true); - return true; }; ResourceType rty = entry.getType().getAnnotation(ResourceType.class); Class[] resTypes = rty == null ? new Class[]{} : rty.value(); diff --git a/src/org/redkale/util/ResourceFactory.java b/src/org/redkale/util/ResourceFactory.java index 2645056cb..c48358de3 100644 --- a/src/org/redkale/util/ResourceFactory.java +++ b/src/org/redkale/util/ResourceFactory.java @@ -349,7 +349,8 @@ public final class ResourceFactory { if (re == null) { ResourceLoader it = findLoader(genctype, field); if (it != null) { - autoregnull = it.load(this, src, rcname, field, attachment); + it.load(this, src, rcname, field, attachment); + autoregnull = it.autoNone(); re = findEntry(rcname, genctype); } } @@ -365,7 +366,8 @@ public final class ResourceFactory { if (re == null) { ResourceLoader it = findLoader(classtype, field); if (it != null) { - autoregnull = it.load(this, src, rcname, field, attachment); + it.load(this, src, rcname, field, attachment); + autoregnull = it.autoNone(); re = findEntry(rcname, classtype); } } @@ -534,8 +536,12 @@ public final class ResourceFactory { @FunctionalInterface public static interface ResourceLoader { - // 返回true 表示如果资源不存在会在ResourceFactory里注入默认值null,返回false表示资源不存在表示每次调用ResourceLoader自行处理 - public boolean load(ResourceFactory factory, Object src, String resourceName, Field field, Object attachment); + public void load(ResourceFactory factory, Object src, String resourceName, Field field, Object attachment); + + // 返回true 表示调用ResourceLoader之后资源仍不存在,则会在ResourceFactory里注入默认值null,返回false表示资源不存在下次仍会调用ResourceLoader自行处理 + default boolean autoNone() { + return true; + } } }