您当前的位置: 首页 >  spring

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

springboot项目启动成功后执行指定方法

小志的博客 发布时间:2020-04-20 10:46:33 ,浏览量:0

一、实现ApplicationRunner接口

1、代码如下: 在这里插入图片描述

package com.rf.config;

import com.rf.mq.MqClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @description: 组件类
 * @author: xiaozhi
 * @create: 2020-04-20 09:24
 */
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("通过实现ApplicationRunner接口,在spring boot项目启动后调用MQClient");
        MqClient mqClient = new MqClient();
        mqClient.connect();
    }
}

2、输出结果的效果图如下: 在这里插入图片描述

二、实现CommandLineRunner接口

1、代码如下: 在这里插入图片描述

package com.rf.config;

import com.rf.mq.MqClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @description: 组件类
 * @author: xiaozhi
 * @create: 2020-04-20 10:41
 */
@Component
public class CommandLineRunnerImpl  implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("通过实现CommandLineRunner接口,在spring boot项目启动后调用MQClient");
        MqClient mqClient = new MqClient();
        mqClient.connect();
    }
}

2、输出的效果图如下: 在这里插入图片描述

三、两种实现方式的不同之处
  • 在于run方法中接收的参数类型不一样
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.1785s