sptingboot---项目启动时自动执行方法

在SpringBoot中,有两种接口方式实现启动执行,分别是ApplicationRunner和CommandLineRunner,除了可接受参数不同,其他的大同小异

CommandLineRunner :

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Component
public class StartMethod implements CommandLineRunner {

    
    @Override
    public void run(String... args) throws Exception {
      //具体的执行方法
    }
}

ApplicationRunner :

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class StartMethod implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments var1) throws Exception{
       //具体的执行方法
    }
}

 

你可能感兴趣的:(SpringBoot)