SpringBoot项目在加载完的时候执行的代码

SpringBoot项目在加载完的时候执行的代码,第1张

实现CommandLineRunner接口,重写run方法,方法里面的内容就是项目加载完要执行的代码
package com.lixianhe.load;

import com.lixianhe.service.DrugServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Controller;

/**
 * @author lixianhe
 * @Date 2022-04-09 14:11
 */
@Controller
public class CommandLineRunnerController implements CommandLineRunner {

    public static int drugMaxCount;

    @Autowired
    private DrugServiceImpl drugService;


    @Override
    public void run(String... args) {
        // 在项目加载的时候给药品最大数量赋值
        drugMaxCount = drugService.getDrugMaxCount();
        System.out.println(drugMaxCount);
        System.out.println(drugService);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/801526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-07
下一篇 2022-05-07

发表评论

登录后才能评论

评论列表(0条)

保存