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{
        // 启动时自动执行的代码
    }
}
相关推荐
HanhahnaH14 分钟前
Spring集合注入Bean
java·spring
未定义.22120 分钟前
电子削铅笔刀顺序图详解:从UML设计到PlantUML实现
java·软件工程·uml
雾月5538 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
丘山子1 小时前
一些鲜为人知的 IP 地址怪异写法
前端·后端·tcp/ip
CopyLower1 小时前
在 Spring Boot 中实现 WebSockets
spring boot·后端·iphone
24k小善2 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者2 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
猿周LV2 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
景天科技苑2 小时前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配