SpringBoot 启动时自动执行指定方法

在Spring Boot应用程序中,要实现在应用启动时自动执行某些代码,可以采用以下几种方式:

1. 使用@PostConstruct注解

@PostConstruct注解用于标记一个方法,该方法将在依赖注入完成后、构造方法之后自动执行。这适用于需要在对象创建后立即执行的初始化逻辑。

bash 复制代码
import javax.annotation.PostConstruct;

@Component
public class UsePostConstruct {

    @PostConstruct
    public void init() {
        // 启动时自动执行的代码
    }
}
2. 实现CommandLineRunner或ApplicationRunner接口

这两个接口都包含了一个run方法,该方法会在Spring应用上下文准备就绪后被调用。ApplicationRunner是CommandLineRunner的增强版,它提供了对命令行参数的访问能力。

bash 复制代码
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class UseCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // 启动时自动执行的代码
    }
}
3. 使用@EventListener注解

@EventListener注解可以用来监听Spring框架的事件。如果你想在Spring容器完全启动后执行某些操作,可以监听ContextRefreshedEvent。

bash 复制代码
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class UseEventListener {

    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 应用上下文初始化完毕后自动执行的代码
    }
}
4. 使用InitializingBean接口

InitializingBean接口提供了一个afterPropertiesSet方法,该方法会在所有属性设置完成后自动执行。

bash 复制代码
import org.springframework.beans.factory.InitializingBean;

public class UseInitializingBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // 启动时自动执行的代码
    }
}
5. 使用ServletContextListener接口

ServletContextListener是一个在Servlet规范中定义的监听器接口,这个接口有个contextInitialized(ServletContextEvent sce)方法是在Web应用被Servlet容器(如Tomcat)加载并初始化时调用。

bash 复制代码
@Component
public class UseServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    	// 启动时自动执行的代码
        ServletContextListener.super.contextInitialized(sce);
        
    }
}
6. 使用ApplicationContextAware接口

ApplicationContextAware是Spring框架中的一个接口,它允许Bean获取到Spring的ApplicationContext。这个接口中只有一个方法setApplicationContext(ApplicationContext applicationContext)在创建这个Bean的实例之后会自动调。

bash 复制代码
@Component
@Slf4j
public class UseApplicationContextAware implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 启动时自动执行的代码
        
    }
}
7. 使用静态代码块

在类中添加静态代码块,这样在Spring在扫描这类时候就会自动执行静态代码,从而达到代码自动运行的效果。

bash 复制代码
@Component
public class UseStatic {

    static{
        // 启动时自动执行的代码
    }
}
相关推荐
garmin Chen2 分钟前
Elasticsearch(4):Java Rest Client 搜索与聚合速查
java·分布式·elasticsearch
小旭95277 分钟前
MySQL 主从复制、MyCat 读写分离与分库分表实战
java·数据库·sql·mysql·database
计算机安禾7 分钟前
【算法分析与设计】第38篇:最近点对与分治在几何中的应用
java·服务器·网络·数据库·算法
IT_陈寒15 分钟前
Vue的动态组件坑了我整整一天!
前端·人工智能·后端
梦梦代码精15 分钟前
功能堆砌不如好扩展:4 款开源商城系统的选型思考
java·docker·uni-app·开源·php
计算机安禾17 分钟前
【算法分析与设计】第37篇:平面扫描与线段交问题
java·大数据·数据库·算法·机器学习
Cobyte19 分钟前
AI 的个人便签纸:Claude Code 的 TodoWrite 模式
前端·后端·aigc
hai31524754320 分钟前
FiveOS V3.0 交付(微服务器操作系统版 · 物理合规修正
linux·人工智能·spring boot·后端·神经网络·机器学习
兰令水22 分钟前
leecodecode【二叉树排序+最近公共祖先】【2026.6.2打卡-java版本】
java·数据结构·算法·leetcode
人道领域22 分钟前
【LeetCode刷题日记】77&&216.回溯算法剪枝优化在组合问题中的应用
java·算法·leetcode