From fd18877d0977163f17b74bf54c1adf1fd3801ee4 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Thu, 22 Mar 2018 15:57:54 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0NbAppEventListener,?= =?UTF-8?q?=E5=8F=AF=E6=8B=A6=E6=88=AANbApp=E7=9A=84=E5=90=84=E7=A7=8D?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChangeLog.md | 4 ++- .../src/main/java/org/nutz/boot/NbApp.java | 30 +++++++++++++++++++ .../nutz/boot/tools/NbAppEventListener.java | 30 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 nutzboot-core/src/main/java/org/nutz/boot/tools/NbAppEventListener.java diff --git a/ChangeLog.md b/ChangeLog.md index 390a3905..0df3d0d2 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,8 +3,10 @@ # dev 迭代中 * 变更: + * update: 完善shiro集成, by 大鲨鱼 - * add: starter-ssdb by 道坤 + * add: starter-ssdb by 道坤 + * add: 添加NbAppEventListener,可以拦截NbApp的各种事件 # 2.1.4 "平凡之路" diff --git a/nutzboot-core/src/main/java/org/nutz/boot/NbApp.java b/nutzboot-core/src/main/java/org/nutz/boot/NbApp.java index 9f6065d5..ed2c8b22 100644 --- a/nutzboot-core/src/main/java/org/nutz/boot/NbApp.java +++ b/nutzboot-core/src/main/java/org/nutz/boot/NbApp.java @@ -20,7 +20,9 @@ import org.nutz.boot.env.SystemPropertiesEnvHolder; import org.nutz.boot.ioc.IocLoaderProvider; import org.nutz.boot.resource.ResourceLoader; import org.nutz.boot.resource.impl.SimpleResourceLoader; +import org.nutz.boot.tools.NbAppEventListener; import org.nutz.boot.tools.PropDocReader; +import org.nutz.boot.tools.NbAppEventListener.EventType; import org.nutz.ioc.Ioc2; import org.nutz.ioc.IocLoader; import org.nutz.ioc.ObjectProxy; @@ -85,6 +87,8 @@ public class NbApp extends Thread { protected boolean prepared; protected Object lock = new Object(); + + protected NbAppEventListener listener = new NbAppEventListener() {}; /** * 创建一个NbApp,把调用本构造方法的类作为mainClass @@ -181,18 +185,26 @@ public class NbApp extends Thread { Stopwatch sw = Stopwatch.begin(); // 各种预备操作 + listener.whenPrepare(this, EventType.before); this.prepare(); + listener.whenPrepare(this, EventType.after); // 依次启动 try { + listener.whenInitAppContext(this, EventType.before); ctx.init(); + listener.whenInitAppContext(this, EventType.after); + listener.whenStartServers(this, EventType.before); ctx.startServers(); + listener.whenStartServers(this, EventType.after); if (ctx.getMainClass().getAnnotation(IocBean.class) != null) ctx.getIoc().get(ctx.getMainClass()); sw.stop(); + + listener.afterAppStated(this); log.infof("NB started : %sms", sw.du()); synchronized (lock) { lock.wait(); @@ -220,19 +232,28 @@ public class NbApp extends Thread { if (prepared) return; // 初始化上下文 + listener.whenPrepareBasic(this, EventType.before); this.prepareBasic(); + listener.whenPrepareBasic(this, EventType.after); // 打印Banner,暂时不可配置具体的类 new SimpleBannerPrinter().printBanner(ctx); // 配置信息要准备好 + + listener.whenPrepareConfigureLoader(this, EventType.before); this.prepareConfigureLoader(); + listener.whenPrepareConfigureLoader(this, EventType.before); // 创建IocLoader体系 + listener.whenPrepareIocLoader(this, EventType.before); prepareIocLoader(); + listener.whenPrepareIocLoader(this, EventType.after); // 加载各种starter + listener.whenPrepareStarterClassList(this, EventType.before); prepareStarterClassList(); + listener.whenPrepareStarterClassList(this, EventType.before); // 打印配置文档 if (printProcDoc) { @@ -242,10 +263,14 @@ public class NbApp extends Thread { } // 创建Ioc容器 + listener.whenPrepareIoc(this, EventType.before); prepareIoc(); + listener.whenPrepareIoc(this, EventType.after); // 生成Starter实例 + listener.whenPrepareStarterInstance(this, EventType.before); prepareStarterInstance(); + listener.whenPrepareStarterInstance(this, EventType.before); prepared = true; } @@ -425,4 +450,9 @@ public class NbApp extends Thread { } } + public void setListener(NbAppEventListener listener) { + if (listener == null) + throw new NullPointerException("NULL NbAppEventListener!!!"); + this.listener = listener; + } } diff --git a/nutzboot-core/src/main/java/org/nutz/boot/tools/NbAppEventListener.java b/nutzboot-core/src/main/java/org/nutz/boot/tools/NbAppEventListener.java new file mode 100644 index 00000000..d072d38a --- /dev/null +++ b/nutzboot-core/src/main/java/org/nutz/boot/tools/NbAppEventListener.java @@ -0,0 +1,30 @@ +package org.nutz.boot.tools; + +import org.nutz.boot.NbApp; + +public interface NbAppEventListener { + + default void whenPrepare(NbApp app, EventType et) {}; + + default void whenPrepareBasic(NbApp app, EventType et) {}; + + default void whenPrepareConfigureLoader(NbApp app, EventType et) {}; + + default void whenPrepareIocLoader(NbApp app, EventType et) {}; + + default void whenPrepareStarterClassList(NbApp app, EventType et) {}; + + default void whenPrepareIoc(NbApp app, EventType et) {}; + + default void whenPrepareStarterInstance(NbApp app, EventType et) {}; + + default void whenInitAppContext(NbApp app, EventType et) {}; + + default void whenStartServers(NbApp app, EventType et) {} + + default void afterAppStated(NbApp app) {}; + + enum EventType { + before, after + } +}