如何实现在SpringBoot项目启动时加载运动特定的代码

前言

如何实现在SpringBoot项目启动类启动时加载运动特定的代码呢 有两种方式

为了方便测试效果,先写一个service在启动类进行注入方便我们输出

package com.wyh.test;

import org.springframework.stereotype.Service;

/**
 * @program: SpringBoot-MybatisPlus-01
 * @description: 我的服务
 * @author: 魏一鹤
 * @createDate: 2021-11-23 23:15
 **/
@Service
public class MyService {
 
        public String startPrint(){
 
            System.out.println("启动类被运行加载会调用我");
            return "ok";
        }
}
//注入我们的service方便实用它的方法
    @Autowired
    private MyService myService;

第一种方式 让我们的启动类实现ApplicationRunner接口 重写它的run方法

implements ApplicationRunner
@Override
    public void run(ApplicationArguments args) throws Exception {
 
        //在SpringBoot启动类启动时会被调用
        myService.startPrint();
        System.out.println("我是第一种方式,实现ApplicationRunner接口 重写它的run方法");
    }

第二种方式 让我们的启动类实现CommandLineRunner接口 重写它的run方法

implements CommandLineRunner
@Override
    public void run(String... args) throws Exception {
 
        //在SpringBoot启动类启动时会被调用
        myService.startPrint();
        System.out.println("我是第二种方式,实现CommandLineRunner接口 重写它的run方法");
    }

开启启动类发现控制台成功打印输出



启动类被运行加载会调用我

我是第一种方式,实现ApplicationRunner接口 重写它的run方法

启动类被运行加载会调用我

我是第二种方式,实现CommandLineRunner接口 重写它的run方法

完整的代码如下

启动类

package com.wyh;

import com.wyh.test.MyService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.wyh.mapper") //扫描我们的mapper接口
//
//1.实现ApplicationRunner接口 重写它的run方法
//2.实现CommandLineRunner接口 重写它的run方法
public class WyhApplication implements ApplicationRunner, CommandLineRunner {
 
    //注入我们的service方便实用它的方法
    @Autowired
    private MyService myService;
    public static void main(String[] args) {
 
        SpringApplication.run(WyhApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
 
        //在SpringBoot启动类启动时会被调用
        myService.startPrint();
        System.out.println("我是第一种方式,实现ApplicationRunner接口 重写它的run方法");
    }

    @Override
    public void run(String... args) throws Exception {
 
        //在SpringBoot启动类启动时会被调用
        myService.startPrint();
        System.out.println("我是第二种方式,实现CommandLineRunner接口 重写它的run方法");
    }
}

service

package com.wyh.test;

import org.springframework.stereotype.Service;

/**
 * @program: SpringBoot-MybatisPlus-01
 * @description: 我的服务
 * @author: 魏一鹤
 * @createDate: 2021-11-23 23:15
 **/
@Service
public class MyService {
 
        public String startPrint(){
 
            System.out.println("启动类被运行加载会调用我");
            return "ok";
        }
}

项目目录



#Java开发实习##学习路径#
全部评论
感谢大佬分享!!!!
点赞 回复 分享
发布于 2022-01-13 15:52

相关推荐

牛客48784610...:深圳的变成录用进行中,这个是稳了吗,还没有收到邮件
点赞 评论 收藏
分享
wu970:标准北漂配置,怎么看着装修风格有点像自如的😭
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务