SpringBoot启动时行初始化方法

本文共 2,963 字,预计阅读时间 10 分钟

 


有时需要在SpringBoot启动时进行一些初始化的操作,有以下几种方法:

1.实现CommandLineRunner接口

在启动类上实现CommandLineRunner接口,重写run方法

复制代码
package com.zxh;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@Slf4j
public class GatewayApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }


    @Value("${server.port}")
    private String port;

    @Override
    public void run(String... args) throws Exception {
        log.info("系统初始化中。。。。。");
        log.info("端口号: {}", port);
    }
}
复制代码

2.使用注解@PostConstruct

新建一个配置类,在要进行初始化的方法上加注解@PostConstruct:

复制代码
package com.zxh.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;


import javax.annotation.PostConstruct;

/**
 * 系统初始化操作
 */
@Configuration
@Slf4j
public class WebAppConfig {

    @Value("${server.port}")
    private String port;

    @PostConstruct
    public void initRun() {
        log.info("系统初始化中。。。。。");
        log.info("端口号: {}", port);
    }

}
复制代码

3.使用注解@EventListener

复制代码
package com.zxh.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;


/**
 * 系统初始化操作
 */
@Configuration
@Slf4j
public class WebAppConfig {

    @Value("${server.port}")
    private String port;


    @EventListener
    public void initRun(ContextRefreshedEvent event) {
        log.info("系统初始化中。。。。。");
        log.info("端口号: {}", port);
    }

}
复制代码

4.实现ApplicationRunner接口

新建类,实现ApplicationRunner接口重写run方法,并将其交给Spring管理

复制代码
package com.zxh.test.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class WebApplicationConfig implements ApplicationRunner {

    @Value("${server.port}")
    private String port;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("系统初始化中。。。。。");
        log.info("端口号: {}", port);
    }
}
复制代码

对比发现,CommandLineRunner和ApplicationRunner都是实现接口,重写其run方法,它们的作用是相同的,但也有一定的区别。主要区别就在于其run方法中的参数,ApplicationRunner中run方法的参数是ApplicationArguments,而CommandLineRunner中run方法的参数是数组,这些参数都是启动SpringBoot程序时传给main()方法的。ApplicationArguments是对main方的参数做了进一步的封装。


上述的几种方法任选一种即可。

posted @   钟小嘿  阅读(448)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2020-09-09 React高级
点击右上角即可分享
微信分享提示