使用ApplicationRunner或CommandLineRunner ExitCodeGenerator

版权声明:dream_on_sakura_rain https://blog.csdn.net/qq_32112175/article/details/82876697

如果想在springboot项目启动之后执行一些代码,可以通过使用ApplicationRunner或CommandLineRunner两种方式实现

https://github.com/licunzhi/dream_on_sakura_rain/tree/master/springboot_initAction_demo github项目代码(里面包含了ExitCodeGenerator的使用案例,可以参考使用),可直接使用

官方文档给出的解释为:

If you need to run some specific code once the SpringApplication has started, you can implement the ApplicationRunner or CommandLineRunner interfaces. Both interfaces work in the same way and offer a single run method, which is called just before SpringApplication.run(…​) completes.

The CommandLineRunner interfaces provides access to application arguments as a simple string array, whereas the ApplicationRunner uses the ApplicationArguments interface discussed earlier. The following example shows a CommandLineRunner with a run method:

意思就是:

如果需要在SpringApplication启动后执行一些特殊的代码,你可以实现ApplicationRunner或CommandLineRunner接口,这两个接口工作方式相同,都只提供单一的run方法,该方法仅在SpringApplication.run(…)完成之前调用。

CommandLineRunner接口能够访问string数组类型的应用参数,而ApplicationRunner使用的是上面描述过的ApplicationArguments接口:

同样官网也给出了代码的案例

import org.springframework.boot.*;
import org.springframework.stereotype.*;

@Component
public class MyBean implements CommandLineRunner {

	public void run(String... args) {
		// Do something...
	}

}

 If several CommandLineRunner or ApplicationRunner beans are defined that must be called in a specific order, you can additionally implement theorg.springframework.core.Ordered interface or use the org.springframework.core.annotation.Order annotation.

如果某些定义的CommandLineRunner或ApplicationRunner beans需要以特定的顺序调用,你可以实现org.springframework.core.Ordered接口或使用org.springframework.core.annotation.Order注解。 

需要注意的是,虽然实现了两者,但是相应的实体类还是需要加上注解@Component,启动进行组件容器管理机制中

 这里面我同时演示了如何定位启动的先后顺序问题@Order

ApplicationRunner

package com.sakura.plum;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author licunzhi
 * @desc 顺序执行①
 * @date 2018-09-28
 */
@Component
@Order(value = 1)
public class FirstInitAction implements ApplicationRunner {

    //实现接口之后重写其中的run方法
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("application runner start ... 1");
        System.out.println(args);
        /*
        * 项目启动之后可以检测数据库信息是否存在,不存在则创建,存在不创建
        * */
    }
}

CommandLineRunner

package com.sakura.plum;

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

/**
 * @author licunzhi
 * @desc 顺序执行②
 * @date 2018-09-28
 */
@Component
@Order(value = 2)
public class SecondAction implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("command linner runner 2");
    }
}

 展示效果

 两者之间还是有区别的

因此我在后续修改了代码的部分,不要问我为什么就是为了展示一下不同的效果而已

运行的效果也是不一样的

欢迎访问交流群:589780530 
博主交流:2718272293
邮箱:[email protected]  [email protected]
github: https://github.com/licunzhi 

猜你喜欢

转载自blog.csdn.net/qq_32112175/article/details/82876697