Yml配置加载优化,@Value注解支持设置默认值.
This commit is contained in:
+16
-2
@@ -37,6 +37,8 @@ import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -44,6 +46,7 @@ import java.util.Set;
|
||||
* @date: 2022/6/16
|
||||
*/
|
||||
public abstract class AbstractConfigurationParser implements ConfigurationParser {
|
||||
private static Pattern pattern = Pattern.compile("\\$\\{[\\s\\p{Zs}]*(.*):(.*)[\\s\\p{Zs}]*\\}");
|
||||
|
||||
@Override
|
||||
public <T> T parse(InputStream in, Class<T> clazz) throws ConfigurationParserException {
|
||||
@@ -109,10 +112,21 @@ public abstract class AbstractConfigurationParser implements ConfigurationParser
|
||||
for (Field field : fields) {
|
||||
String key = field.getName();
|
||||
Value value = field.getAnnotation(Value.class);
|
||||
Object defaultValue = null;
|
||||
if (null != value && StringUtil.notEmpty(value.value())) {
|
||||
key = value.value();
|
||||
Matcher m = pattern.matcher(value.value());
|
||||
if (m.find()) {
|
||||
key = m.group(1);
|
||||
defaultValue = m.group(2);
|
||||
} else {
|
||||
key = value.value();
|
||||
}
|
||||
}
|
||||
boolean success = ReflectUtil.setFieldValue(field, instance, config.get(key));
|
||||
Object fieldValue = config.get(key);
|
||||
if (null == fieldValue) {
|
||||
fieldValue = defaultValue;
|
||||
}
|
||||
boolean success = ReflectUtil.setFieldValue(field, instance, fieldValue);
|
||||
if (!success) {
|
||||
try {
|
||||
Object o = parseProxy((Map)config.get(key), field.getType());
|
||||
|
||||
@@ -47,6 +47,8 @@ public class ApplicationConfig {
|
||||
|
||||
@Data
|
||||
public static class Http {
|
||||
private Boolean enable;
|
||||
@Value("${port:9999}")
|
||||
private Integer port;
|
||||
@Value("context-path")
|
||||
private String contextPath;
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ public class ExtensionServiceLoader implements ApplicationRunner {
|
||||
return;
|
||||
}
|
||||
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
|
||||
if (null == http || null == http.getPort()) {
|
||||
if (null == http || null == http.getEnable() || !http.getEnable()) {
|
||||
return;
|
||||
}
|
||||
applicationBeanFactory.registerBean(WebApplicationServer.class);
|
||||
|
||||
@@ -21,7 +21,9 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.web;
|
||||
|
||||
import fun.asgc.neutrino.core.annotation.Autowired;
|
||||
import fun.asgc.neutrino.core.annotation.Component;
|
||||
import fun.asgc.neutrino.core.context.ApplicationConfig;
|
||||
import fun.asgc.neutrino.core.context.ApplicationRunner;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -33,6 +35,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WebApplicationServer implements ApplicationRunner {
|
||||
@Autowired
|
||||
private ApplicationConfig rootApplicationConfig;
|
||||
|
||||
@Override
|
||||
public void run(String[] args) {
|
||||
|
||||
@@ -23,6 +23,9 @@ package fun.asgc.neutrino.core.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: aoshiguchen
|
||||
@@ -32,7 +35,15 @@ public class ClassUtilTest {
|
||||
|
||||
@Test
|
||||
public void hasNoArgsConstructor() {
|
||||
System.out.println(ClassUtil.hasNoArgsConstructor(ClassUtilTest.class));
|
||||
// System.out.println(ClassUtil.hasNoArgsConstructor(ClassUtilTest.class));
|
||||
Pattern pattern = Pattern.compile("\\$\\{[\\s\\p{Zs}]*(.*):(.*)[\\s\\p{Zs}]*\\}");
|
||||
String s = "${ a:bb11 }";
|
||||
Matcher m = pattern.matcher(s);
|
||||
if (m.find()) {
|
||||
System.out.println(m.group(1));
|
||||
System.out.println(m.group(2));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ neutrino:
|
||||
application:
|
||||
name: neutrino-proxy-server
|
||||
http:
|
||||
port: 8080
|
||||
enable: true
|
||||
port:
|
||||
context-path: /
|
||||
|
||||
proxy:
|
||||
|
||||
Reference in New Issue
Block a user