springboot项目开机自启动 预加载数据,自动打开登陆页面,swagger接口文档

CommandLineRunner

  • 在应用服务启动时,需要在所有Bean生成之后,预先加载一些数据和执行一些应用的初始化
    例如:删除临时文件,清楚缓存信息,读取配置文件,数据库连接,这些工作类似开机自启动的概念
  • CommandLineRunner、ApplicationRunner
    接口是在容器启动成功后的最后一步回调(类似开机自启动)就可以完成这些工作。
package org.springframework.boot;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 * <p>
 * If you need access to {@link ApplicationArguments} instead of the raw String array
 * consider using {@link ApplicationRunner}.
 *
 * @author Dave Syer
 * @see ApplicationRunner
 */
@FunctionalInterface
public interface CommandLineRunner {

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}
该接口指明:
1.CommandLineRunner是一个函数式接口,我们需要时,只需实现该接口就行,只要在应用中被声明成了bean就会执行run方法。
2.如果在一个应用上下文中存在多个bean,我们也可以使用@Order注解来排序。
3.如果你需要使用ApplicationArguments参数替代数组参数,请使用ApplicationRunner接口

自动打开登陆页面,swagger接口文档

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.context.WebApplicationContext;

import java.io.IOException;

@SpringBootApplication(exclude={DruidDataSourceAutoConfigure.class, DataSourceAutoConfiguration.class})
public class JdbctemplateApplication implements CommandLineRunner {

    @Value("${server.port}")
    String port;

    @Autowired
    WebApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(JdbctemplateApplication.class, args);
    }

    @Override
    public void run(String... args)
            throws IOException
    {
        if (SystemUtils.IS_OS_WINDOWS)
        {
            String url = String.format("http://127.0.0.1:%s/%s", port, applicationContext.getServletContext().getContextPath());
            Runtime.getRuntime().exec("cmd /c start /min " + url);
            Runtime.getRuntime().exec("cmd /c start /min " + url + "swagger-ui.html");
        }
    }

}
代码说明
1.实现CommandLineRunner接口
2.判断系统运行系统是WINDOWS,Runtime.getRuntime()获取当前应用运行环境对象,执行cmd脚本命令

预加载数据

@Slf4j
@Component
@Order(2)
public class StartPreLoading implements CommandLineRunner {

    //在应用内的bean初始化后运行,所有的bean在此都能获取到
    @Autowired
    private ThreadPoolTaskExecutor executor;

    @Override
    public void run(String... args) throws Exception {
        log.info("StartPreLoading ........");
    }
}
@Slf4j
@Component
@Order(1)
public class StartPreLoading2 implements CommandLineRunner {

    @Autowired
    private ThreadPoolTaskExecutor executor;

    @Override
    public void run(String... args) throws Exception {
        log.info("StartPreLoading2 ........");
    }
}

从应用启动日志可以看出,在应用加载完成后,就会执行这两个预加载的bean中的run方法,且按照先@Order(1)后@Order(2)的顺序加载,@Order中的数字越小,就会被先加载

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大佬腿好粗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值