Solon2 之基础:四、应用启动过程与完整生命周期

串行的处理过程(含六个事件扩展点 + 两个函数扩展点),代码直接、没有什么模式。易明

提醒:

  • 启动过程完成后,项目才能正常运行(启动过程中,不能把线程卡死了)
  • AppBeanLoadEndEvent 之前的事件,需要启动前通过 EventBus.subscribe(...) 订阅

1、事件订阅示例

  • AppLoadEndEvent
@Component
public class  AppLoadEndEventListener implements EventListener<AppLoadEndEvent>{
    @Override
    public void onEvent(AppLoadEndEvent event) throws Throwable {
        //event.app(); //获取应用对象
    }
}
  • AppStopEndEvent,v2.1.0 后支持
@Component
public class  AppStopEndEventListener implements EventListener<AppStopEndEvent>{
    @Override
    public void onEvent(AppStopEndEvent event) throws Throwable {
        //event.app(); //获取应用对象
    }
}

2、插件在应用生命周期里的时机点

插件的本质,即在应用生命周期中获得关键执行时机的接口。从而有效获得应用扩展能力。

  • 插件接口 Plugin
@FunctionalInterface
public interface Plugin {
    void start(AopContext context) throws Throwable;
    default void prestop() throws Throwable{}
    default void stop() throws Throwable{}
}
  • 执行时机
接口执行时机说明
start在 7 时机点执行启动
prestop在 ::stop 前执行预停止
stop在 ::ShutdownHook 时执行停止(启用安全停止时,prestop 后等几秒再执行 stop)

3、注解能力注册的合适时机点

  • 比如,时机点5
public class DemoApp{
    public void static main(String[] args){
        Solon.start(DemoApp.clas, args, app->{
            //比如注册Demo注解
            Solon.context().beanAroundAdd(DemoAop.class, new DemoInterceptor());
        });
    }
}
  • 比如,时机点6(借用 SolonBuilder,提前注册事件)
public class DemoApp{
    public void static main(String[] args){
        new SolonBuilder().onAppInitEnd(e -> {
            //...时机点6
        }).onAppLoadEnd(e->{
            //...时间点e          
        }).start(JobApp.class, args);
    }
}

定义一个插件

public class DemoPluginImp implements Plugin {
    @Override
    public void start(AopContext context) {
        //比如注册Demo注解
        context.beanAroundAdd(DemoAop.class, new DemoInterceptor());
    }
}

//可通过[时机点5]注册插件
public class DemoApp{
    public void static main(String[] args){
        Solon.start(DemoApp.clas, args, app->{
            app.pluginAdd(0, new DemoPluginImp()); //此处注册的插件,会在[时机点7]运行
        });
    }
}

//或可通过 app.yml 的配置,借用[时机点4]申明插件
//solon.plugin: "xxx.xxx.DemoPluginImp"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有什么问题吗:INFO 2023-07-22 23:43:48.754 [-main][*][o.noear.solon.Solon]: App: Plugin starting INFO 2023-07-22 23:43:48.937 [-main][*][o.noear.solon.Solon]: Session: Local session state plugin is loaded INFO 2023-07-22 23:43:49.256 [-main][*][o.noear.solon.Solon]: View: load: FreemarkerRender INFO 2023-07-22 23:43:49.258 [-main][*][o.noear.solon.Solon]: View: load: org.noear.solon.view.freemarker.FreemarkerRender INFO 2023-07-22 23:43:49.258 [-main][*][o.noear.solon.Solon]: View: mapping: .ftl=FreemarkerRender INFO 2023-07-22 23:43:49.292 [-main][*][o.noear.solon.Solon]: App: Bean scanning INFO 2023-07-22 23:43:50.099 [-main][*][o.noear.solon.Solon]: View: mapping: .html=FreemarkerRender INFO 2023-07-22 23:43:50.995 [-main][*][o.noear.solon.Solon]: Connector:main: undertow: Started ServerConnector@{HTTP/1.1,[http/1.1]}{http://localhost:8080} INFO 2023-07-22 23:43:50.995 [-main][*][o.noear.solon.Solon]: Server:main: undertow: Started (undertow 2.2.24/2.3.8) @893ms INFO 2023-07-22 23:43:50.997 [-main][*][o.noear.solon.Solon]: View: mapping: @json=StringSerializerRender#SnackSerializer INFO 2023-07-22 23:43:50.997 [-main][*][o.noear.solon.Solon]: View: mapping: @type_json=StringSerializerRender#SnackSerializer INFO 2023-07-22 23:43:56.851 [-main][*][c.c.c.InitConfig]: nginxIsRun:false INFO 2023-07-22 23:43:56.899 [-main][*][c.c.c.InitConfig]: runCmd:nginx -c /home/nginxWebUI/nginx.conf INFO 2023-07-22 23:43:57.055 [-main][*][c.c.c.InitConfig]: _ _ __ __ __ __ ____ ____ ____ _ (_)____ _ __| | / /___ / /_ / / / // _/ / __ \ / __ `// // __ \ | |/_/| | /| / // _ \ / __ \ / / / / / / / / / // /_/ // // / / /_> < | |/ |/ // __// /_/ // /_/ /_/ / /_/ /_/ \__, //_//_/ /_//_/|_| |__/|__/ \___//_.___/ \____//___/ /____/
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值