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

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

1. 使用@PostConstruct注解

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

import javax.annotation.PostConstruct;

@Component
public class UsePostConstruct {

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

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

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。

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方法,该方法会在所有属性设置完成后自动执行。

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)加载并初始化时调用。

@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的实例之后会自动调。

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

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

@Component
public class UseStatic {

    static{
        // 启动时自动执行的代码
    }
}

相关推荐

  1. SpringBoot 启动自动执行指定方法

    2024-03-14 14:48:04       22 阅读
  2. spring启动自动执行方法

    2024-03-14 14:48:04       22 阅读
  3. SpringBoot项目启动执行指定方法的四种实现

    2024-03-14 14:48:04       28 阅读
  4. springboot】项目启动打印全部接口方法

    2024-03-14 14:48:04       15 阅读
  5. cmake执行指定参数

    2024-03-14 14:48:04       15 阅读
  6. Linux登录/重启自动执行

    2024-03-14 14:48:04       44 阅读
  7. Springboot项目启动成功后可通过五种方式继续执行

    2024-03-14 14:48:04       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 14:48:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 14:48:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 14:48:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 14:48:04       20 阅读

热门阅读

  1. C++ 之emplace_back 和 push_back

    2024-03-14 14:48:04       18 阅读
  2. Vue中的知识点

    2024-03-14 14:48:04       23 阅读
  3. CXL (Compute Express Link) Technology——论文阅读

    2024-03-14 14:48:04       36 阅读
  4. c++面经

    2024-03-14 14:48:04       19 阅读
  5. 人类的谋算与量子计算

    2024-03-14 14:48:04       15 阅读
  6. 用C语言链表实现图书管理

    2024-03-14 14:48:04       18 阅读
  7. 算法刷题day30:递归

    2024-03-14 14:48:04       23 阅读
  8. Dijkstra&floyed

    2024-03-14 14:48:04       20 阅读
  9. 3. Linux标准I/O库

    2024-03-14 14:48:04       18 阅读