Resource增加required属性
This commit is contained in:
@@ -31,6 +31,13 @@ public @interface Resource {
|
|||||||
// */
|
// */
|
||||||
// APPLICATION
|
// APPLICATION
|
||||||
// }
|
// }
|
||||||
|
//
|
||||||
|
/**
|
||||||
|
* 是否必须存在
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public boolean required() default false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 资源名称
|
* 资源名称
|
||||||
|
|||||||
@@ -748,9 +748,12 @@ public final class ResourceFactory {
|
|||||||
register(rcname, genctype, null); //自动注入null的值
|
register(rcname, genctype, null); //自动注入null的值
|
||||||
re = findEntry(rcname, genctype);
|
re = findEntry(rcname, genctype);
|
||||||
}
|
}
|
||||||
if (re == null) continue;
|
if (re != null) {
|
||||||
re.elements.add(new ResourceElement<>(srcObj, field));
|
re.elements.add(new ResourceElement<>(srcObj, field));
|
||||||
rs = re.value;
|
rs = re.value;
|
||||||
|
} else {
|
||||||
|
rs = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (rs != null && !rs.getClass().isPrimitive() && classtype.isPrimitive()) {
|
if (rs != null && !rs.getClass().isPrimitive() && classtype.isPrimitive()) {
|
||||||
if (classtype == int.class) {
|
if (classtype == int.class) {
|
||||||
@@ -770,9 +773,14 @@ public final class ResourceFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (rs != null) field.set(srcObj, rs);
|
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);
|
} while ((clazz = clazz.getSuperclass()) != Object.class);
|
||||||
return true;
|
return true;
|
||||||
|
} catch (ResourceInjectException e) {
|
||||||
|
throw e;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.log(Level.SEVERE, "inject " + srcObj + " error", ex);
|
logger.log(Level.SEVERE, "inject " + srcObj + " error", ex);
|
||||||
return false;
|
return false;
|
||||||
@@ -825,6 +833,25 @@ public final class ResourceFactory {
|
|||||||
return it == null ? findRegxTypeLoader(ft, field) : it;
|
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<T> {
|
private static class ResourceEntry<T> {
|
||||||
|
|
||||||
public final String name;
|
public final String name;
|
||||||
|
|||||||
Reference in New Issue
Block a user