SpringBoot启动时执行指定任务

时间:2022-07-22
本文章向大家介绍SpringBoot启动时执行指定任务,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一个比较好的案例需求:在jar包运行成功后自启动浏览器页面

核心注解:

@Configuration

@EventListener({ApplicationReadyEvent.class})

具体实现

/**
 * @author 乐心湖
 * @date 2020/7/21 14:04
 **/
@Configuration
public class AutoBrower {

    @EventListener({ApplicationReadyEvent.class})
    public void applicationReadyEvent() {
        System.out.println("主控程序部署完毕 ... 启动可视化界面");
        String url = "xxx";
        startIndex(url);
    }

    /**
     * 启动首页
     * @param url
     */
    public void startIndex(String url){
        String index = url + "/index.html";
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}