ResourceFactory优化

This commit is contained in:
redkale
2024-02-01 08:54:58 +08:00
parent 5ce226edd7
commit cd059c02a2
4 changed files with 64 additions and 23 deletions

View File

@@ -17,6 +17,7 @@ import java.util.function.*;
import java.util.logging.*;
import org.redkale.annotation.*;
import org.redkale.convert.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.inject.spi.ResourceAnnotationProvider;
import org.redkale.util.Creator;
import org.redkale.util.RedkaleClassLoader;
@@ -806,6 +807,18 @@ public final class ResourceFactory {
if (name == null) {
return null;
}
if (name.startsWith("${")) {
String subName = name.substring(2);
int pos = subName.lastIndexOf('}');
if (pos > 0) {
subName = subName.substring(0, pos);
pos = subName.indexOf(':');
if (pos > 0) {
subName = subName.substring(0, pos);
}
name = subName;
}
}
int pos = name.indexOf("{system.property.");
if (pos < 0) {
return (name.contains(Resource.PARENT_NAME) && parent != null) ? name.replace(Resource.PARENT_NAME, parent) : name;
@@ -821,6 +834,22 @@ public final class ResourceFactory {
return getResourceName(parent, prefix + System.getProperty(property, "") + postfix);
}
private static String getResourceDefaultValue(String parent, String name) {
if (name.startsWith("${")) {
String subName = name.substring(2);
int pos = subName.lastIndexOf('}');
if (pos > 0) {
subName = subName.substring(0, pos);
pos = subName.indexOf(':');
if (pos > 0) {
String val = subName.substring(pos + 1);
return "null".equals(val) ? null : val;
}
}
}
return null;
}
private <T> boolean inject(String srcResourceName, Object srcObj, T attachment, BiConsumer<Object, Field> consumer, List<Object> list) {
if (srcObj == null) {
return false;
@@ -922,6 +951,7 @@ public final class ResourceFactory {
}
boolean autoRegNull = true;
final String rcname = getResourceName(srcResourceName, tname);
final String defval = getResourceDefaultValue(srcResourceName, tname);
Object rs = null;
if (rcname.startsWith("system.property.")) {
rs = System.getProperty(rcname.substring("system.property.".length()));
@@ -1003,6 +1033,9 @@ public final class ResourceFactory {
rs = re.value;
}
}
if (rs == null && defval != null) {
rs = gencType == String.class ? defval : JsonConvert.root().convertFrom(gencType, defval);
}
if (rs != null && !rs.getClass().isPrimitive() && (classType.isPrimitive()
|| classType == Integer.class
|| classType == Long.class || classType == Short.class

View File

@@ -13,6 +13,7 @@ import java.nio.charset.*;
import java.util.*;
import java.util.function.Supplier;
import java.util.logging.Level;
import org.redkale.annotation.ClassDepends;
import org.redkale.annotation.Comment;
import org.redkale.convert.*;
import org.redkale.convert.json.JsonConvert;
@@ -20,7 +21,6 @@ import org.redkale.net.Request;
import org.redkale.util.*;
import static org.redkale.util.Utility.isEmpty;
import static org.redkale.util.Utility.isNotEmpty;
import org.redkale.annotation.ClassDepends;
/**
* Http请求包 与javax.servlet.http.HttpServletRequest 基本类似。 <br>
@@ -1547,7 +1547,8 @@ public class HttpRequest extends Request<HttpContext> {
+ (this.getHost() != null ? (", \r\n host: " + this.host) : "")
+ (this.getContentLength() >= 0 ? (", \r\n contentLength: " + this.contentLength) : "")
+ (this.array.length() > 0 ? (", \r\n bodyLength: " + this.array.length()) : "")
+ (this.boundary || this.array.isEmpty() ? "" : (", \r\n bodyContent: " + (this.respConvertType == null || this.respConvertType == ConvertType.JSON ? this.getBodyUTF8() : Arrays.toString(getBody()))))
+ (this.boundary || this.array.isEmpty() ? "" : (", \r\n bodyContent: "
+ (this.respConvertType == null || this.respConvertType == ConvertType.JSON ? this.getBodyUTF8() : Arrays.toString(getBody()))))
+ ", \r\n params: " + toMapString(this.params.map, 4)
+ ", \r\n header: " + toMapString(this.headers.map, 4)
+ "\r\n}"; //this.headers.toString(4)
@@ -1583,7 +1584,7 @@ public class HttpRequest extends Request<HttpContext> {
final InputStream in = newInputStream();
return new MultiContext(context.getCharset(), this.getContentType(), this.params.map(),
new BufferedInputStream(in, Math.max(array.length(), 8192)) {
{
{
array.copyTo(this.buf);
this.count = array.length();
}