如何实现在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方法");
    }

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

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

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

我是第一种方式,实现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";
        }
}

项目目录

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值