优化Configuration

This commit is contained in:
redkale
2024-08-08 17:19:44 +08:00
parent b6659ea15e
commit aa47820ee7
3 changed files with 37 additions and 6 deletions

View File

@@ -708,7 +708,18 @@ public final class Application {
} catch (IOException e) {
throw new RedkaleException(e);
}
filter.getFilterEntrys().forEach(en -> resourceFactory.register(en.getType()));
StringBuilder sb = new StringBuilder();
filter.getFilterEntrys().forEach(en -> {
int c = resourceFactory.registerConfiguration(en.getType());
sb.append("Load Configuration (type=")
.append(en.getType().getName())
.append(") ")
.append(c)
.append(" resources\r\n");
});
if (sb.length() > 0) {
logger.log(Level.INFO, sb.toString().trim());
}
}
private void registerResourceEnvs(boolean first, Properties... envs) {

View File

@@ -625,7 +625,8 @@ public final class ResourceFactory {
* @param configuareClass 标记Configuration的类
*
*/
public void registerConfiguration(final Class configuareClass) {
public int registerConfiguration(final Class configuareClass) {
int count = 0;
Object instance = Creator.create(configuareClass).create();
for (Method method : configuareClass.getDeclaredMethods()) {
Resource res = method.getAnnotation(Resource.class);
@@ -647,13 +648,14 @@ public final class ResourceFactory {
String resName = getResourceName(res.name());
Type resType = method.getGenericReturnType();
method.setAccessible(true);
Object obj = Modifier.isStatic(method.getModifiers()) ? null : instance;
Supplier resSupplier = () -> {
Object[] args = new Object[paramSuppliers.length];
for (int i = 0; i < paramSuppliers.length; i++) {
args[i] = paramSuppliers[i].get();
}
try {
return method.invoke(instance, args);
return method.invoke(obj, args);
} catch (Exception e) {
throw new RedkaleException(method + " invoke error", e);
}
@@ -662,7 +664,9 @@ public final class ResourceFactory {
resConfigureSupplierMap
.computeIfAbsent(resType, k -> new ConcurrentHashMap<>())
.put(resName, resSupplier);
count++;
}
return count;
}
/**
@@ -1460,13 +1464,13 @@ public final class ResourceFactory {
try {
oldVal = element.field.get(dest);
} catch (Throwable e) {
//do nothing
// do nothing
}
}
try {
element.field.set(dest, newVal);
} catch (Throwable e) {
//do nothing
// do nothing
}
if (element.changedMethod != null) {
try {

View File

@@ -27,12 +27,17 @@ public class ConfigurationTest {
factory.register("a.name", "my a name");
factory.register("b.id", 4321);
factory.register("b.name", "my b name");
factory.register("c.id", 8888);
factory.register("c.name", "my c name");
factory.register("c.desc", "my desc");
factory.registerConfiguration(DiyConfiguration.class);
BeanB pb = new BeanB();
factory.inject(pb);
Assertions.assertEquals(new BeanA(1234, "my a name", "auto").toString(), pb.bean.toString());
Assertions.assertEquals(new BeanA(8888, "my c name", "my desc").toString(), pb.beanc.toString());
System.out.println(pb.bean);
System.out.println(pb.beanc);
}
@Configuration
@@ -40,17 +45,28 @@ public class ConfigurationTest {
@Resource(name = "a")
BeanA createBeanA() {
System.out.println("创建一个Bean");
System.out.println("创建一个a Bean");
BeanA bean = new BeanA();
bean.desc = "auto";
return bean;
}
@Resource(name = "c")
static BeanA createBeanC(@Resource(name = "c.desc") String desc) {
System.out.println("创建一个c Bean");
BeanA bean = new BeanA();
bean.desc = desc;
return bean;
}
}
public static class BeanB {
@Resource(name = "a")
public BeanA bean;
@Resource(name = "c")
public BeanA beanc;
}
public static class BeanA {