diff --git a/pom.xml b/pom.xml index 63f4b5a..5e610a7 100644 --- a/pom.xml +++ b/pom.xml @@ -6,25 +6,12 @@ com.imyeyu.inject timi-inject - 0.0.1 + 0.0.1-legacy - 21 - 21 + 8 + 8 UTF-8 true - - - - com.imyeyu.io - timi-io - 0.0.1 - - - ch.qos.logback - logback-classic - 1.5.13 - - diff --git a/src/main/java/com/imyeyu/inject/CallbackArg.java b/src/main/java/com/imyeyu/inject/CallbackArg.java new file mode 100644 index 0000000..bed3c3a --- /dev/null +++ b/src/main/java/com/imyeyu/inject/CallbackArg.java @@ -0,0 +1,10 @@ +package com.imyeyu.inject; + +/** + * @author 夜雨 + * @since 2025-07-23 22:50 + */ +public interface CallbackArg { + + void handler(T t); +} diff --git a/src/main/java/com/imyeyu/inject/InjectApp.java b/src/main/java/com/imyeyu/inject/InjectApp.java index 0619d45..65bf275 100644 --- a/src/main/java/com/imyeyu/inject/InjectApp.java +++ b/src/main/java/com/imyeyu/inject/InjectApp.java @@ -1,7 +1,5 @@ package com.imyeyu.inject; -import com.imyeyu.java.bean.CallbackArg; - import java.util.ArrayList; import java.util.List; @@ -17,8 +15,6 @@ public final class InjectApp { TimiInject injector; - boolean enableBanner = true; - /** 全局注入后监听,包括静态注入 */ final List> afterInjectCallbackList = new ArrayList<>(); @@ -31,11 +27,6 @@ public final class InjectApp { this.clazz = clazz; } - public InjectApp disableBanner() { - enableBanner = false; - return this; - } - public TimiInject injector() { return injector; } @@ -57,12 +48,4 @@ public final class InjectApp { public synchronized void removeAfterInjectListener(CallbackArg listener) { afterInjectCallbackList.remove(listener); } - - public boolean isEnableBanner() { - return enableBanner; - } - - public void setEnableBanner(boolean enableBanner) { - this.enableBanner = enableBanner; - } } diff --git a/src/main/java/com/imyeyu/inject/TimiInject.java b/src/main/java/com/imyeyu/inject/TimiInject.java index bcd0cb8..f5b2da5 100644 --- a/src/main/java/com/imyeyu/inject/TimiInject.java +++ b/src/main/java/com/imyeyu/inject/TimiInject.java @@ -15,12 +15,6 @@ import com.imyeyu.inject.annotation.SuperIOC; import com.imyeyu.inject.annotation.SuperInject; import com.imyeyu.inject.annotation.TimiInjectApplication; import com.imyeyu.inject.annotation.Util; -import com.imyeyu.io.IO; -import com.imyeyu.java.TimiJava; -import com.imyeyu.java.bean.CallbackArg; -import com.imyeyu.utils.Time; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; @@ -31,6 +25,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URI; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.Enumeration; @@ -49,9 +44,6 @@ import java.util.jar.JarFile; */ public class TimiInject implements InjectFactory { - /** 日志 */ - private static final Logger log = LoggerFactory.getLogger(TimiInject.class); - /** 类路径 */ private final List paths; @@ -79,21 +71,13 @@ public class TimiInject implements InjectFactory { private final Object iocAsyncLock = new Object(); private TimiInject(InjectApp app) { - if (TimiJava.isEmpty(app)) { - throw new NullPointerException("empty inject app"); + if (app == null) { + throw new NullPointerException("not found inject app"); } app.injector = this; // 初始化 - long initAt = Time.now(); - // Logo - if (app.enableBanner) { - if (IO.resourceExist(getClass(), "banner.txt")) { - log.info(IO.resourceToString(getClass(), "banner.txt")); - } else { - log.info(IO.resourceToString(getClass(), "defBanner.txt")); - } - } - log.info("Starting TimiInject {} ..", app.clazz); + long initAt = System.currentTimeMillis(); + System.out.printf("Starting TimiInject %s ..%n", app.clazz); List scanPathList = new ArrayList<>(); objects = new HashMap<>(); iocListeners = new HashMap<>(); @@ -104,18 +88,18 @@ public class TimiInject implements InjectFactory { classes = new ArrayList<>(); try { // 扫描 - long scanAt = Time.now(); + long scanAt = System.currentTimeMillis(); { // 扫描包 - log.info("Scanning.."); + System.out.println("Scanning.."); TimiInjectApplication appAnnotation = app.clazz.getAnnotation(TimiInjectApplication.class); if (appAnnotation == null) { throw new NullPointerException("TimiInjectApplication can not be null, used annotation for scanner class"); } - if (TimiJava.isEmpty(appAnnotation.value())) { - scanPathList.add(app.clazz.getPackageName()); + if (appAnnotation.value() == null || appAnnotation.value().length == 0) { + scanPathList.add(app.clazz.getPackage().getName()); } else { - scanPathList.addAll(List.of(appAnnotation.value())); + scanPathList.addAll(Arrays.asList(appAnnotation.value())); } // 扫描类 URI baseURI = app.clazz.getProtectionDomain().getCodeSource().getLocation().toURI(); @@ -125,8 +109,8 @@ public class TimiInject implements InjectFactory { } } // 控制反转 - log.info("Running IOC.."); - long iocAt = Time.now(); + System.out.println("Running IOC.."); + long iocAt = System.currentTimeMillis(); { // Application IOC if (app.obj != null) { @@ -173,7 +157,7 @@ public class TimiInject implements InjectFactory { newInstance(iocClass); } } catch (Exception e) { - throw new RuntimeException("IOC fail id: %s, class: %s".formatted(iocClass.id, iocClass.clazz), e); + throw new RuntimeException(String.format("IOC fail id: %s, class: %s", iocClass.id, iocClass.clazz), e); } } if (hasAsyncIOC && iocAsyncRunning != 0) { @@ -184,8 +168,8 @@ public class TimiInject implements InjectFactory { } } // 依赖注入 - log.info("Running inject.."); - long injectAt = Time.now(); + System.out.println("Running inject.."); + long injectAt = System.currentTimeMillis(); { Collection objects = this.objects.values(); for (Object object : objects) { @@ -201,21 +185,14 @@ public class TimiInject implements InjectFactory { app.afterInjectCallbackList.get(j).handler(this); } } - log.info("Invoking injected callback.."); + System.out.println("Invoking injected callback.."); // 注入调度 - long invokeForInjectedAt = Time.now(); + long invokeForInjectedAt = System.currentTimeMillis(); { for (Map.Entry> item : invokeForInjected.entrySet()) { for (Method method : item.getValue().keySet()) { try { - long start = -1; - if (log.isDebugEnabled()) { - start = Time.now(); - } method.invoke(item.getKey()); - if (log.isDebugEnabled()) { - log.debug("invoke injected event {} ms for {}#{}", "%4s".formatted(Time.now() - start), item.getKey().getClass().getName(), method.getName()); - } } catch (Exception e) { throw new RuntimeException("invoke method fail: " + item.getKey().getClass().getName() + "#" + method.getName(), e); } @@ -239,19 +216,19 @@ public class TimiInject implements InjectFactory { } } - long now = Time.now(); - log.info("Completed!"); - log.info("IOC objects: " + objects.size()); - log.info("Inject fields: " + fields.size()); - log.info("Build Time: "); - log.info("{} ms for Initialization", "%8s".formatted(scanAt - initAt)); - log.info("{} ms for Scan Classes", "%8s".formatted(iocAt - scanAt)); - log.info("{} ms for IOC", "%8s".formatted(injectAt - iocAt)); - log.info("{} ms for Inject", "%8s".formatted(invokeForInjectedAt - injectAt)); - log.info("{} ms for Invoke Callback", "%8s".formatted(now - invokeForInjectedAt)); - log.info("{} ms for Total", "%8s".formatted(now - initAt)); + long now = System.currentTimeMillis(); + System.out.println("Completed!"); + System.out.printf("IOC objects: %s%n", objects.size()); + System.out.printf("Inject fields: %s%n", fields.size()); + System.out.println("Build Time: "); + System.out.printf("%s ms for Initialization%n", scanAt - initAt); + System.out.printf("%s ms for Scan Classes%n", iocAt - scanAt); + System.out.printf("%s ms for IOC%n", injectAt - iocAt); + System.out.printf("%s ms for Inject%n", invokeForInjectedAt - injectAt); + System.out.printf("%s ms for Invoke Callback%n", now - invokeForInjectedAt); + System.out.printf("%s ms for Total%n", now - initAt); } catch (Exception e) { - log.error("TimiInject run error", e); + e.printStackTrace(); } } @@ -382,28 +359,14 @@ public class TimiInject implements InjectFactory { * @throws IllegalAccessException 访问异常 */ private void newInstance(IOCClass iocClass) throws Exception { - long start = -1; - if (log.isDebugEnabled()) { - start = Time.now(); - } - Constructor constructor = iocClass.clazz.getDeclaredConstructor(); constructor.setAccessible(true); Object object = constructor.newInstance(); - if (TimiJava.isEmpty(iocClass.id)) { + if (iocClass.id == null || iocClass.id.isEmpty() ) { objects.put(iocClass.path, object); } else { objects.put(iocClass.id, object); } - - if (log.isDebugEnabled()) { - if (iocClass.isAsync) { - log.debug("IOC {} ms(async) for {}: {}", "%4s".formatted(Time.now() - start), iocClass.id, iocClass.clazz.getName()); - } else { - log.debug("IOC {} ms for {}: {}", "%4s".formatted(Time.now() - start), iocClass.id, iocClass.clazz.getName()); - } - } - // 控制反转方法返回 objects.putAll(iocReturn(object.getClass(), object)); // 收集此对象需要注入后调度的方法 @@ -433,25 +396,16 @@ public class TimiInject implements InjectFactory { final int j = i; new Thread(() -> { try { - long start = -1; - if (log.isDebugEnabled()) { - start = Time.now(); - } - Object returnObject = methods[j].invoke(object); if (returnObject == null) { throw new NullPointerException("return IOC object can not be null: " + clazz.getSimpleName() + "#" + methods[j].getName()); } String id = iocReturn.value(); - if (TimiJava.isEmpty(id)) { + if (id == null || id.isEmpty()) { id = returnObject.getClass().getName() + "#" + methods[j].getName(); } objects.put(id, returnObject); - if (log.isDebugEnabled()) { - log.debug("IOC {} ms(async) for {}: {}", "%4s".formatted(Time.now() - start), id, returnObject.getClass().getName()); - } - synchronized (iocAsyncLock) { iocAsyncRunning--; if (iocAsyncRunning < 1) { @@ -462,32 +416,25 @@ public class TimiInject implements InjectFactory { synchronized (iocAsyncLock) { iocAsyncLock.notifyAll(); } - log.error("IOC fail class: %s#%s".formatted(clazz.getName(), methods[j].getName()), e); + System.err.printf("IOC fail class: %s#%s%n", clazz.getName(), methods[j].getName()); + e.printStackTrace(); } }).start(); } else { - long start = -1; - if (log.isDebugEnabled()) { - start = Time.now(); - } - Object returnObject = methods[i].invoke(object); if (returnObject == null) { throw new NullPointerException("return IOC object can not be null: " + clazz.getSimpleName() + "#" + methods[i].getName()); } String id = iocReturn.value(); - if (TimiJava.isEmpty(id)) { + if (id == null || id.isEmpty()) { id = returnObject.getClass().getName() + "#" + methods[i].getName(); } iocObjects.put(id, returnObject); - - if (log.isDebugEnabled()) { - log.debug("IOC {} ms for {}: {}", "%4s".formatted(Time.now() - start), id, returnObject.getClass().getName()); - } } } } catch (InvocationTargetException | IllegalAccessException e) { - log.error("IOC fail class: %s#%s".formatted(clazz.getName(), methods[i].getName()), e); + System.err.printf("IOC fail class: %s#%s%n", clazz.getName(), methods[i].getName()); + e.printStackTrace(); } } return iocObjects; @@ -530,10 +477,8 @@ public class TimiInject implements InjectFactory { } else { String value = inject.value(); // 执行注入 - if (TimiJava.isEmpty(value)) { + if (value == null || value.isEmpty()) { String id = fields[i].getType().getName() + "#" + fields[i].getName(); - - log.debug("Injecting {}", id); Object diObject = di(id, Object.class); try { if (diObject == null) { @@ -657,9 +602,9 @@ public class TimiInject implements InjectFactory { @Override public void ioc(String id, Object object) { try { - log.info("Call IOC: " + id); + System.out.println("Call IOC: " + id); // ---------- 控制反转 ---------- - long iocAt = Time.now(); + long iocAt = System.currentTimeMillis(); // 本类对象 objects.put(id, object); // 方法返回对象 @@ -667,7 +612,7 @@ public class TimiInject implements InjectFactory { objects.putAll(iocReturnObjects); // ---------- 注入 ---------- - long injectAt = Time.now(); + long injectAt = System.currentTimeMillis(); // 本类对象字段 inject(object, object.getClass()); // 其他类的本类对象注入 @@ -684,14 +629,14 @@ public class TimiInject implements InjectFactory { } } } - long afterInjectingAt = Time.now(); + long afterInjectingAt = System.currentTimeMillis(); // 一般注入后调度 Map methods = invokeForInjected(object.getClass()); for (Map.Entry method : methods.entrySet()) { method.getKey().invoke(object); } - long callIOCListenersAt = Time.now(); + long callIOCListenersAt = System.currentTimeMillis(); // 控制反转监听 if (iocListeners.get(object.getClass()) != null) { List> listeners = iocListeners.get(object.getClass()); @@ -699,17 +644,11 @@ public class TimiInject implements InjectFactory { listeners.get(i).handler(object); } } - long now = Time.now(); - log.info("Completed for {} IOC object in {} ms", (iocReturnObjects.size() + 1), now - iocAt); - - log.debug("Build Time Detail: "); - log.debug("{} ms for IOC", "%8s".formatted(injectAt - iocAt)); - log.debug("{} ms for Inject", "%8s".formatted(afterInjectingAt - injectAt)); - log.debug("{} ms for Injected Event", "%8s".formatted(callIOCListenersAt - afterInjectingAt)); - log.debug("{} ms for IOC Listener Event", "%8s".formatted(now - callIOCListenersAt)); - log.debug("{} ms for Total", "%8s".formatted(now - iocAt)); + long now = System.currentTimeMillis(); + System.out.printf("Completed for %s IOC object in %s ms%n", iocReturnObjects.size() + 1, now - iocAt); } catch (Exception e) { - log.error("call ioc fail: " + id, e); + System.err.println("call ioc fail: " + id); + e.printStackTrace(); } }