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{
        // 启动时自动执行的代码
    }
}
相关推荐
在未来等你2 分钟前
互联网大厂Java求职面试:AI大模型推理优化与实时数据处理架构
java·ai·大模型·向量数据库·rag·分布式系统
买了一束花5 分钟前
预分配矩阵内存提升文件数据读取速度
java·人工智能·算法·matlab
zizisuo17 分钟前
Java集合框架深度剖析:结构、并发与设计模式全解析
java·javascript·数据结构·设计模式
要加油哦~23 分钟前
刷题 | 牛客 - js中等题-下(更ing)30/54知识点&解答
java·开发语言·javascript
gkdpjj34 分钟前
Linux网络 网络基础一
linux·服务器·开发语言·网络·后端·智能路由器·软件工程
程序员鱼皮1 小时前
炸裂!Spring AI 1.0 正式发布,让 Java 再次伟大!
java·计算机·ai·程序员·互联网·开发
向上的车轮1 小时前
Spring Boot微服务架构(四):微服务的划分原则
spring boot·微服务·架构
俺不是西瓜太郎´•ﻌ•`1 小时前
欧拉降幂(JAVA)蓝桥杯乘积幂次
java·开发语言·蓝桥杯
神码小Z1 小时前
Spring Cloud Gateway 微服务网关实战指南
java·spring boot·spring cloud
EQ-雪梨蛋花汤2 小时前
【如何做好一份技术文档?】用Javadoc与PlantUML构建高质量技术文档(API文档自动化部署)
java·api·ci·plantuml·doc