优化Configuration

This commit is contained in:
redkale
2024-08-08 16:49:29 +08:00
parent cca73a5045
commit b6659ea15e
11 changed files with 67 additions and 35 deletions

View File

@@ -0,0 +1,49 @@
/*
*/
package org.redkale.annotation;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
/**
* 启动服务时的初始化配置,需要结合{@link org.redkale.annotation.Resource}使用
* <blockquote>
* <pre>
* &#064;Configuration
* public class MyConfiguration {
*
* &#064;Resource(name = "a")
* BeanA createBeanA() {
* System.out.println("创建一个Bean");
* BeanA bean = new BeanA();
* bean.desc = "auto";
* return bean;
* }
*
* &#064;Resource(name = "b")
* BeanA createBeanA(&#064;Resource(name = "dev.desc") String desc) {
* System.out.println("创建一个Bean");
* BeanA bean = new BeanA();
* bean.desc = name;
* return bean;
* }
* }
*
* </pre>
* </blockquote>
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
* @since 2.8.0
*/
@Documented
@Target({TYPE})
@Retention(RUNTIME)
public @interface Configuration {}

View File

@@ -20,6 +20,7 @@ import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.logging.*;
import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.Configuration;
import org.redkale.annotation.Nonnull;
import org.redkale.asm.AsmMethodBoost;
import org.redkale.boot.ClassFilter.FilterEntry;
@@ -33,7 +34,6 @@ import org.redkale.convert.Convert;
import org.redkale.convert.bson.BsonFactory;
import org.redkale.convert.json.*;
import org.redkale.convert.proto.ProtobufFactory;
import org.redkale.inject.Configuration;
import org.redkale.inject.ResourceEvent;
import org.redkale.inject.ResourceFactory;
import org.redkale.inject.ResourceTypeLoader;
@@ -702,12 +702,13 @@ public final class Application {
}
});
// 加载Configuration
ClassFilter<Configuration> filter = new ClassFilter(this.getClassLoader(), Configuration.class);
ClassFilter<?> filter = new ClassFilter(this.getClassLoader(), Configuration.class, Object.class);
try {
loadClassByFilters(filter);
} catch (IOException e) {
throw new RedkaleException(e);
}
filter.getFilterEntrys().forEach(en -> resourceFactory.register(en.getType()));
}
private void registerResourceEnvs(boolean first, Properties... envs) {

View File

@@ -74,7 +74,6 @@ public class ServerWatchService extends AbstractWatchService {
try {
server.changeAddress(application, newAddr);
} catch (IOException e) {
e.printStackTrace();
return new RetResult(RET_SERVER_CHANGEPORT_ERROR, "changeAddress error");
}
return RetResult.success();

View File

@@ -1317,7 +1317,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
RedkaleClassLoader.putReflectionMethod(subclazz.getName(), method);
break;
} catch (Exception e) {
e.printStackTrace();
// do nothing
}
}
if (simpleCoder != null) {
@@ -1462,7 +1462,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
RedkaleClassLoader.putReflectionMethod(subclazz.getName(), method);
break;
} catch (Exception e) {
e.printStackTrace();
// do nothing
}
}
if (simpleCoder != null) {

View File

@@ -37,7 +37,6 @@ public class URISimpledCoder<R extends Reader, W extends Writer> extends Simpled
try {
return new URI(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

View File

@@ -37,7 +37,6 @@ public class URLSimpledCoder<R extends Reader, W extends Writer> extends Simpled
try {
return new URL(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

View File

@@ -1,15 +0,0 @@
/*
*/
package org.redkale.inject;
/**
* 启动服务时的初始化配置,需要结合{@link org.redkale.annotation.Resource}使用
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
* @since 2.8.0
*/
public interface Configuration {}

View File

@@ -620,14 +620,13 @@ public final class ResourceFactory {
}
/**
* 注册 Configuration
* 注册Configuration配置
*
* @param <T> 泛型
* @param configuareClass Configuration的实现类
* @param configuareClass 标记Configuration的类
*
*/
public <T extends Configuration> void register(final Class<T> configuareClass) {
Configuration instance = Creator.create(configuareClass).create();
public void registerConfiguration(final Class configuareClass) {
Object instance = Creator.create(configuareClass).create();
for (Method method : configuareClass.getDeclaredMethods()) {
Resource res = method.getAnnotation(Resource.class);
if (res == null) {
@@ -1461,13 +1460,13 @@ public final class ResourceFactory {
try {
oldVal = element.field.get(dest);
} catch (Throwable e) {
e.printStackTrace();
//do nothing
}
}
try {
element.field.set(dest, newVal);
} catch (Throwable e) {
e.printStackTrace();
//do nothing
}
if (element.changedMethod != null) {
try {

View File

@@ -1844,7 +1844,7 @@ public final class Rest {
} catch (ClassNotFoundException e) {
// do nothing
} catch (Throwable e) {
e.printStackTrace();
//do nothing
}
// ------------------------------------------------------------------------------
final String defModuleName = getWebModuleNameLowerCase(serviceType);

View File

@@ -347,7 +347,7 @@ public final class Utility {
}
}
} catch (Throwable e) { // 不会发生
e.printStackTrace();
// do nothing
}
}
unsafeInstance = unsafe0;
@@ -3598,7 +3598,7 @@ public final class Utility {
}
}
} catch (Exception e) {
e.printStackTrace();
// do nothing
}
return back;
}

View File

@@ -4,9 +4,9 @@
package org.redkale.test.inject;
import org.junit.jupiter.api.*;
import org.redkale.annotation.Configuration;
import org.redkale.annotation.Resource;
import org.redkale.convert.json.JsonFactory;
import org.redkale.inject.Configuration;
import org.redkale.inject.ResourceFactory;
/**
@@ -27,7 +27,7 @@ public class ConfigurationTest {
factory.register("a.name", "my a name");
factory.register("b.id", 4321);
factory.register("b.name", "my b name");
factory.register(DiyConfiguration.class);
factory.registerConfiguration(DiyConfiguration.class);
BeanB pb = new BeanB();
factory.inject(pb);
@@ -35,7 +35,8 @@ public class ConfigurationTest {
System.out.println(pb.bean);
}
public static class DiyConfiguration implements Configuration {
@Configuration
public static class DiyConfiguration {
@Resource(name = "a")
BeanA createBeanA() {