diff --git a/src/main/java/javax/annotation/Resource.java b/src/main/java/javax/annotation/Resource.java index fc879c954..05628eb96 100644 --- a/src/main/java/javax/annotation/Resource.java +++ b/src/main/java/javax/annotation/Resource.java @@ -31,6 +31,13 @@ public @interface Resource { // */ // APPLICATION // } +// + /** + * 是否必须存在 + * + * @return boolean + */ + public boolean required() default false; /** * 资源名称 diff --git a/src/main/java/org/redkale/util/ResourceFactory.java b/src/main/java/org/redkale/util/ResourceFactory.java index 83ee1262f..8a5abf02b 100644 --- a/src/main/java/org/redkale/util/ResourceFactory.java +++ b/src/main/java/org/redkale/util/ResourceFactory.java @@ -748,9 +748,12 @@ public final class ResourceFactory { register(rcname, genctype, null); //自动注入null的值 re = findEntry(rcname, genctype); } - if (re == null) continue; - re.elements.add(new ResourceElement<>(srcObj, field)); - rs = re.value; + if (re != null) { + re.elements.add(new ResourceElement<>(srcObj, field)); + rs = re.value; + } else { + rs = null; + } } if (rs != null && !rs.getClass().isPrimitive() && classtype.isPrimitive()) { if (classtype == int.class) { @@ -770,9 +773,14 @@ public final class ResourceFactory { } } if (rs != null) field.set(srcObj, rs); + if (rs == null && rc.required()) { + throw new ResourceInjectException("resource(type=" + field.getType().getSimpleName() + ", field=" + field.getName() + ", name=" + rcname + ") must exists in " + srcObj.getClass().getName()); + } } } while ((clazz = clazz.getSuperclass()) != Object.class); return true; + } catch (ResourceInjectException e) { + throw e; } catch (Exception ex) { logger.log(Level.SEVERE, "inject " + srcObj + " error", ex); return false; @@ -825,6 +833,25 @@ public final class ResourceFactory { return it == null ? findRegxTypeLoader(ft, field) : it; } + private static class ResourceInjectException extends RuntimeException { + + public ResourceInjectException() { + super(); + } + + public ResourceInjectException(String s) { + super(s); + } + + public ResourceInjectException(String message, Throwable cause) { + super(message, cause); + } + + public ResourceInjectException(Throwable cause) { + super(cause); + } + } + private static class ResourceEntry { public final String name;