优化Configuration
This commit is contained in:
@@ -708,7 +708,18 @@ public final class Application {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RedkaleException(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) {
|
private void registerResourceEnvs(boolean first, Properties... envs) {
|
||||||
|
|||||||
@@ -625,7 +625,8 @@ public final class ResourceFactory {
|
|||||||
* @param configuareClass 标记Configuration的类
|
* @param configuareClass 标记Configuration的类
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void registerConfiguration(final Class configuareClass) {
|
public int registerConfiguration(final Class configuareClass) {
|
||||||
|
int count = 0;
|
||||||
Object instance = Creator.create(configuareClass).create();
|
Object instance = Creator.create(configuareClass).create();
|
||||||
for (Method method : configuareClass.getDeclaredMethods()) {
|
for (Method method : configuareClass.getDeclaredMethods()) {
|
||||||
Resource res = method.getAnnotation(Resource.class);
|
Resource res = method.getAnnotation(Resource.class);
|
||||||
@@ -647,13 +648,14 @@ public final class ResourceFactory {
|
|||||||
String resName = getResourceName(res.name());
|
String resName = getResourceName(res.name());
|
||||||
Type resType = method.getGenericReturnType();
|
Type resType = method.getGenericReturnType();
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
|
Object obj = Modifier.isStatic(method.getModifiers()) ? null : instance;
|
||||||
Supplier resSupplier = () -> {
|
Supplier resSupplier = () -> {
|
||||||
Object[] args = new Object[paramSuppliers.length];
|
Object[] args = new Object[paramSuppliers.length];
|
||||||
for (int i = 0; i < paramSuppliers.length; i++) {
|
for (int i = 0; i < paramSuppliers.length; i++) {
|
||||||
args[i] = paramSuppliers[i].get();
|
args[i] = paramSuppliers[i].get();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return method.invoke(instance, args);
|
return method.invoke(obj, args);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RedkaleException(method + " invoke error", e);
|
throw new RedkaleException(method + " invoke error", e);
|
||||||
}
|
}
|
||||||
@@ -662,7 +664,9 @@ public final class ResourceFactory {
|
|||||||
resConfigureSupplierMap
|
resConfigureSupplierMap
|
||||||
.computeIfAbsent(resType, k -> new ConcurrentHashMap<>())
|
.computeIfAbsent(resType, k -> new ConcurrentHashMap<>())
|
||||||
.put(resName, resSupplier);
|
.put(resName, resSupplier);
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1460,13 +1464,13 @@ public final class ResourceFactory {
|
|||||||
try {
|
try {
|
||||||
oldVal = element.field.get(dest);
|
oldVal = element.field.get(dest);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
//do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
element.field.set(dest, newVal);
|
element.field.set(dest, newVal);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
//do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
if (element.changedMethod != null) {
|
if (element.changedMethod != null) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -27,12 +27,17 @@ public class ConfigurationTest {
|
|||||||
factory.register("a.name", "my a name");
|
factory.register("a.name", "my a name");
|
||||||
factory.register("b.id", 4321);
|
factory.register("b.id", 4321);
|
||||||
factory.register("b.name", "my b name");
|
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);
|
factory.registerConfiguration(DiyConfiguration.class);
|
||||||
|
|
||||||
BeanB pb = new BeanB();
|
BeanB pb = new BeanB();
|
||||||
factory.inject(pb);
|
factory.inject(pb);
|
||||||
Assertions.assertEquals(new BeanA(1234, "my a name", "auto").toString(), pb.bean.toString());
|
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.bean);
|
||||||
|
System.out.println(pb.beanc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -40,17 +45,28 @@ public class ConfigurationTest {
|
|||||||
|
|
||||||
@Resource(name = "a")
|
@Resource(name = "a")
|
||||||
BeanA createBeanA() {
|
BeanA createBeanA() {
|
||||||
System.out.println("创建一个Bean");
|
System.out.println("创建一个a Bean");
|
||||||
BeanA bean = new BeanA();
|
BeanA bean = new BeanA();
|
||||||
bean.desc = "auto";
|
bean.desc = "auto";
|
||||||
return bean;
|
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 {
|
public static class BeanB {
|
||||||
|
|
||||||
@Resource(name = "a")
|
@Resource(name = "a")
|
||||||
public BeanA bean;
|
public BeanA bean;
|
||||||
|
|
||||||
|
@Resource(name = "c")
|
||||||
|
public BeanA beanc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BeanA {
|
public static class BeanA {
|
||||||
|
|||||||
Reference in New Issue
Block a user