Springboot - 在啟動完成后執行特定方法


1.實現方式

  1. 實現ApplicationRunner接口
  2. 實現CommandLineRunner接口
    @Component
    @Slf4j
    public class AfterServiceStarted implements ApplicationRunner{
    
        /**
         * 會在服務啟動完成后立即執行
         */
        @Override
        public void run(ApplicationArguments args) throws Exception {
    
            log.info("Successful service startup!");
        }
    }
    @Component
    public class AfterServiceStartedOther implements CommandLineRunner{
    
        /**
         * 會在服務啟動完成后立即執行
         */
        @Override
        public void run(String... args) throws Exception {
    
            JedisSingleton.getInstance().set("Service startup time", String.valueOf(System.nanoTime()));
        }
    }

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM