From d3a2da3e760ece87d24f5f409801f689e57c2bd2 Mon Sep 17 00:00:00 2001 From: biezhi Date: Mon, 26 Nov 2018 16:55:04 +0800 Subject: [PATCH] :bug: fixed #295 --- pom.xml | 2 +- src/main/java/com/blade/Blade.java | 2 +- .../com/blade/ioc/bean/ValueInjector.java | 38 ++++++++++++------- src/test/java/com/blade/model/AppInfo.java | 10 ++--- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 2e35719bd..59a17d96c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bladejava blade-mvc - 2.0.12.ALPHA + 2.0.13-SNAPSHOT jar blade diff --git a/src/main/java/com/blade/Blade.java b/src/main/java/com/blade/Blade.java index ee9f8a2c3..63d4b0eeb 100644 --- a/src/main/java/com/blade/Blade.java +++ b/src/main/java/com/blade/Blade.java @@ -849,11 +849,11 @@ public Blade start() { */ public Blade start(Class mainCls, String... args) { try { - Assert.packageNotEmpty(mainCls,"your main class is empty of package."); this.loadConfig(args); this.bootClass = mainCls; eventManager.fireEvent(EventType.SERVER_STARTING, new Event().attribute("blade", this)); + Thread thread = new Thread(() -> { try { server.start(Blade.this); diff --git a/src/main/java/com/blade/ioc/bean/ValueInjector.java b/src/main/java/com/blade/ioc/bean/ValueInjector.java index 826ad421b..584ce416c 100644 --- a/src/main/java/com/blade/ioc/bean/ValueInjector.java +++ b/src/main/java/com/blade/ioc/bean/ValueInjector.java @@ -2,6 +2,8 @@ import com.blade.Environment; import com.blade.ioc.Injector; +import com.blade.kit.ReflectKit; +import com.blade.kit.StringKit; import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Field; @@ -15,8 +17,8 @@ @Slf4j public class ValueInjector implements Injector { private Environment environment; - private Field target; - private String key; + private Field target; + private String key; public ValueInjector(Environment environment, Field target, String key) { this.environment = environment; @@ -30,32 +32,40 @@ public void injection(Object bean) { if (!key.isEmpty()) { Class clazz = target.getType(); target.setAccessible(true); - Optional value = environment.get(key); - if (!value.isPresent()) { + Optional fieldValue = environment.get(key); + if (!fieldValue.isPresent()) { log.warn("config is absent,so can't be injected:target is {}", bean.getClass().getName()); return; } - if (value.get().isEmpty()) { + + if (fieldValue.get().isEmpty()) { log.warn("config is empty,so can't be injected:target is {}", bean.getClass().getName()); return; } - //target field type is String - if (clazz.isAssignableFrom(String.class)) { - target.set(bean, value.isPresent() ? value.get() : ""); - return; + + Object value = null; + + //target field type is Basic Type + if (ReflectKit.isBasicType(clazz)) { + if (fieldValue.isPresent() && StringKit.isNotBlank(fieldValue.get())) { + value = ReflectKit.convert(clazz, fieldValue.get()); + } + if (null != value) { + ReflectKit.setFieldValue(target, bean, value); + } } //List and Map support,just support String element - String split = environment.get("value.split", ","); + String split = environment.get("value.split", ","); String mapSplit = environment.get("value.map.split", ":"); if (clazz.isAssignableFrom(List.class)) { - target.set(bean, Arrays.asList(value.get().split(split))); + target.set(bean, Arrays.asList(fieldValue.get().split(split))); return; } - Map map = new HashMap(16); + Map map = new HashMap<>(16); if (clazz.isAssignableFrom(Map.class)) { - Arrays.stream(value.get().split(split)) - .filter(d -> d.indexOf(mapSplit) != -1) + Arrays.stream(fieldValue.get().split(split)) + .filter(d -> d.contains(mapSplit)) .map(d -> d.split(mapSplit)) .forEach(keyValue -> map.put(keyValue[0], keyValue[1])); target.set(bean, map); diff --git a/src/test/java/com/blade/model/AppInfo.java b/src/test/java/com/blade/model/AppInfo.java index a2c322a56..0d14e4952 100644 --- a/src/test/java/com/blade/model/AppInfo.java +++ b/src/test/java/com/blade/model/AppInfo.java @@ -10,10 +10,10 @@ @Value(name = "app") @Data public class AppInfo { - private String users; - private String maxMoney; - private String sex; - private String hits; + private int users; + private double maxMoney; + private boolean sex; + private Long hits; private String startDate; - + private int age; }