zoukankan      html  css  js  c++  java
  • Spring Boot 5:应用程序启动时初始化资源

    需求:应用程序启动后,初始化基础数据、加密证书等操作。

    可以使用CommandLineRunner接口来实现,在SpringBoot.run()之后完成资源的初始化工作。

    注意:多个Runner需要顺序启动的话,可以使用@Order注解

    package sun.flower.diver.modules.system.init;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * 应用程序启动后加载基础数据 Runner
     *
     * @Author YangXuyue
     * @Date 2018/10/28 13:48
     */
    @Component
    @Order(1)
    public class BaseDataRunner implements CommandLineRunner {
        @Override
        public void run(String... strings) throws Exception {
            System.out.println("start init base data");
        }
    }
    package sun.flower.diver.modules.system.init;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * 应用程序启动后加载证书 Runner
     *
     * @Author YangXuyue
     * @Date 2018/10/28 13:50
     */
    @Component
    @Order(2)
    public class CertificateRunner implements CommandLineRunner {
        @Override
        public void run(String... strings) throws Exception {
            System.out.println("start init certificate info");
        }
    }
    package sun.flower.diver;
    
    import org.springframework.boot.Banner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    // 添加注解 @EnableDiscoveryClient,只有这样,服务注册、心跳检测相关配置信息才能被自动加载
    @EnableDiscoveryClient
    public class DiverApplication {
    
        public static void main(String[] args) {
            System.out.println("application start");
    
            SpringApplication application = new SpringApplication(DiverApplication.class);
            // 添加监听器,此时监听器类不需要标注是一个Bean
            //application.addListeners(new BaseListener());
            application.setBannerMode(Banner.Mode.OFF);
            application.run(args);
    
            System.out.println("application started");
        }
    
    }

  • 相关阅读:
    BZOJ5302: [Haoi2018]奇怪的背包
    BZOJ5303: [Haoi2018]反色游戏
    UOJ#217. 【UNR #1】奇怪的线段树(广义线段树性质+上下界最小流)
    Codeforces 702F T-Shirts(平衡树+复杂度分析)
    满足决策单调性的 DP 的通用做法
    JZOJ 6754. 2020.07.18【NOI2020】模拟T3 (树链剖分+分治+闵科夫斯基和)
    JZOJ 6756. 2020.07.21【NOI2020】模拟T2 (搜索有用状态+背包dp)
    JZOJ 6757 2020.07.21【NOI2020】模拟T3 (至少容斥+OGF+NTT)
    线性规划转对偶问题
    GDOI 2020 赛前反思
  • 原文地址:https://www.cnblogs.com/yang21/p/9865399.html
Copyright © 2011-2022 走看看