springboot启动后自定义初始化操作

springboot项目启动之后,需要初始化一下自定义的工具类,记录一下

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

@Component
public class TestRunner implements CommandLineRunner {

    @Autowired
    private RocketMqConsumerUtil rocketMqConsumerUtil;

    @Override
    public void run(String... args) throws Exception {
        rocketMqConsumerUtil.listener();
    }
}

另外还找到了另一种方法,记录一下

import xxx.util.RocketMqConsumerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner ;
import org.springframework.stereotype.Component;

@Component
@Order(2)
public class TestRunner implements ApplicationRunner {

    @Autowired
    private RocketMqConsumerUtil rocketMqConsumerUtil;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        rocketMqConsumerUtil.listener();
    }
}

如果要初始化很多自定义的配置类,并且这些配置有先后初始化顺序,可以使用注解@Order(1) 来进行初始化的顺序

posted @ 2021-09-02 17:30  ジ绯色月下ぎ  阅读(289)  评论(0编辑  收藏  举报