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();
}

View File

@@ -6,10 +6,10 @@ import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.*;
import org.redkale.annotation.Resource;
import org.redkale.annotation.ResourceChanged;
import org.redkale.inject.ResourceEvent;
import org.redkale.inject.ResourceFactory;
import org.redkale.util.*;
import org.redkale.annotation.ResourceChanged;
/**
*
@@ -22,12 +22,12 @@ public class ResourceListenerTest {
public static void main(String[] args) throws Throwable {
ResourceListenerTest test = new ResourceListenerTest();
test.main = true;
test.run();
test.run1();
test.run2();
}
@Test
public void run() throws Exception {
AtomicInteger aCounter = new AtomicInteger();
public void run1() throws Exception {
Properties env = new Properties();
env.put("property.id", "2345");
ResourceFactory factory = ResourceFactory.create();
@@ -46,26 +46,35 @@ public class ResourceListenerTest {
prop.put("property.name", "my name");
factory.register(prop, "", Environment.class);
if (!main) {
Assertions.assertTrue(aservice.counter.get() == 1);
Assertions.assertTrue(bservice.counter.get() == 2);
Assertions.assertTrue(abservice.counter.get() == 2);
}
Assertions.assertEquals("7890", aservice.id);
Assertions.assertTrue(aservice.counter.get() == 1);
Assertions.assertTrue(bservice.counter.get() == 2);
Assertions.assertTrue(abservice.counter.get() == 2);
factory.register("property.id", "7777");
if (!main) {
Assertions.assertTrue(aservice.counter.get() == 2);
Assertions.assertTrue(bservice.counter.get() == 2);
Assertions.assertTrue(abservice.counter.get() == 3);
}
Assertions.assertTrue(aservice.counter.get() == 2);
Assertions.assertTrue(bservice.counter.get() == 2);
Assertions.assertTrue(abservice.counter.get() == 3);
}
@Test
public void run2() throws Exception {
Properties env = new Properties();
ResourceFactory factory = ResourceFactory.create();
factory.register(new Environment(env));
AService aservice = new AService();
factory.inject(aservice);
Assertions.assertEquals("33", aservice.id);
factory.register("property.id", "7777");
Assertions.assertEquals("7777", aservice.id);
}
class AService {
public final AtomicInteger counter = new AtomicInteger();
@Resource(name = "property.id", required = false)
@Resource(name = "${property.id:33}", required = false)
private String id;
@Resource(name = "property.desc", required = false)

View File

@@ -13,11 +13,9 @@ import org.redkale.inject.ResourceFactory;
*/
public class ResourceLoaderTest {
private boolean main;
public static void main(String[] args) throws Throwable {
ResourceLoaderTest test = new ResourceLoaderTest();
test.main = true;
test.run();
}
@@ -34,7 +32,7 @@ public class ResourceLoaderTest {
ParentBean pb = new ParentBean();
factory.inject(pb);
if (!main) Assertions.assertEquals(new Bean(1234, "my a name").toString(), pb.bean.toString());
Assertions.assertEquals(new Bean(1234, "my a name").toString(), pb.bean.toString());
System.out.println(pb.bean);
}