Spring Boot项目指定启动后执行的操作

Spring Boot项目指定启动后执行的操作:

(1)实现CommandLineRunner 接口

(2)重写run方法

(3)声明执行顺序@Order(1),数值越小,优先级越高

(4)如果需要注入service或者component等类,再加上@Component注解

package com.googosoft.gateway_zuul;

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

/**
 * 该类在springboot启动之后执行run方法
 * @author songyan
 * @date 2020年2月27日
 */
@Component
@Order(1)
public class ApplicationInitor implements CommandLineRunner {
     
    @Value("${monitorPlatformUrl}")
    private String monitorPlatformUrl;

    @Override
    public void run(String... args) throws Exception {
        //启动后执行该方法
    }
     
}
原文地址:https://www.cnblogs.com/excellencesy/p/12373614.html