SpringBoot启动执行代码

SpringBoot启动执行代码
废话少说上代码

@Component
@Slf4j
public class LimsCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        printInfo();
        //openBrowser();
    }
    private void openBrowser(){
        try {
            if (ProfileUtils.getActiveProfile().equals(ProfileUtils.DEV)){
                Runtime.getRuntime().exec("cmd   /c   start   http://localhost:9999/doc.html");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    private void printInfo(){
        ApplicationContext context = SpringContextHolder.getContext();
        Environment env = context.getEnvironment();

        String[] profiles = env.getActiveProfiles();
        log.info("当前运行环境:{}", profiles[0]);

        String ip = "";
        try{
            ip = InetAddress.getLocalHost().getHostAddress();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path","");

        log.info("本地Swagger文档:http://localhost:{}{}/doc.html",port,path);
        log.info("内网Swagger文档:http://{}:{}{}/doc.html",ip,port,path);
    }
}

我这里使用的是CommandLineRunner接口,实现CommandLineRunner接口需要重写run()方法。我在我代码里实现了项目启动后打印出Swagger地址。
如果有多个方法需要执行可以通过增加注解 @Order(value=?) 来指定执行顺序,从而帮助我们更好的书写代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值