优化Configuration
This commit is contained in:
49
src/main/java/org/redkale/annotation/Configuration.java
Normal file
49
src/main/java/org/redkale/annotation/Configuration.java
Normal 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>
|
||||
* @Configuration
|
||||
* public class MyConfiguration {
|
||||
*
|
||||
* @Resource(name = "a")
|
||||
* BeanA createBeanA() {
|
||||
* System.out.println("创建一个Bean");
|
||||
* BeanA bean = new BeanA();
|
||||
* bean.desc = "auto";
|
||||
* return bean;
|
||||
* }
|
||||
*
|
||||
* @Resource(name = "b")
|
||||
* BeanA createBeanA(@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 {}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user