SpringBoot 在啓動時運行代碼 按順序執行不同業務

當前springboot  爲按順序執行3個不同的類

1.開始執行 

package com.soft.startup;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @ClassName AfterServiceStarted
 * @Description 項目啓動過程中執行一次 順序爲5
 * @Author issuser
 * @Date 2019/9/11 8:29
 * @Version 1.0.0
 */
@Order(5)
@Component
public class AfterServiceStarted implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
    }
}

2. 然後執行

package com.soft.startup;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Properties;
import static io.github.biezhi.ome.OhMyEmail.SMTP_QQ;

/**
 * @ClassName AfterEmailService
 * @Description 項目啓動過程中執行一次 啓動順序爲6
 * @Author issuser
 * @Date 2019/9/11 8:30
 * @Version 1.0.0
 */
@Order(6)
@Component
public class AfterEmailService implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
       
        System.out.println("AfterEmailService ...");
    }
}

3.最後執行

package com.soft.startup;

import io.github.biezhi.ome.OhMyEmail;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import static io.github.biezhi.ome.OhMyEmail.SMTP_QQ;

/**
 * @ClassName AfterEmailService
 * @Description 項目啓動過程中執行一次 啓動順序爲7
 * @Author issuser
 * @Date 2019/9/11 8:30
 * @Version 1.0.0
 */
@Order(7)
@Component
public class AfterOtherService implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章