Springboot 启动后自动执行某个方法

211 阅读5分钟

需求说明

在实际项目开发过程中,我们有时候需要让项目在启动时执行特定方法。如要实现这些功能

1.提前加载相应的数据到缓存中;

2.检查当前项目运行环境:

3.检查程序授权信息,若未授权则不能使用后续功能

4.执行某个特定方法;

总结

执行顺序是:

  1. 静态代码块 // 不能使用类中注入度Bean
  2. ServletContextListener接口contextInitialized方法 // 不能使用类中注入的Bean
  3. ServletContextAware接口setServletContext 方法 // 不能使用类中注入的Bean
  4. @PostConstruct注解方式 // 下面的可以使用类中注入的Bean
  5. @EventListener方式
  6. ApplicationRunner接口run 方法
  7. CommandLineRunner接口run 方法

推荐@PostConstruct注解方式

在Spring Boot应用中,项目启动时执行的不同初始化方法按照以下顺序执行:

  1. 静态代码块

    • 这是在类加载时执行的,因此是所有初始化步骤中最先执行的部分。
  2. ServletContextListener接口的contextInitialized()方法

    • 作为Servlet规范的一部分,在应用服务器创建ServletContext时触发。这通常在Spring容器初始化之前发生。
  3. ServletContextAware接口的setServletContext()方法

    • 在Spring容器获取到ServletContext之后调用,发生在ServletContextListener之后。
  4. @PostConstruct注解的方法

    • 这些方法在Bean被实例化并完成依赖注入后执行,属于Spring容器初始化的一部分,因此在上述步骤之后进行。
  5. @EventListener方式监听的事件(如ContextRefreshedEvent)

    • 当应用上下文准备好后发布事件,对应的listener方法随后执行。
  6. ApplicationRunner接口的run()方法

    • 在 SpringApplication.run() 方法中处理,通常在CommandLineRunner之前执行。
  7. CommandLineRunner接口的run()方法

    • 同样在 SpringApplication.run() 中处理,在默认情况下紧随ApplicationRunner之后执行。

总结:

  • 静态代码块 → ServletContextListener → ServletContextAware → @PostConstruct → @EventListener → ApplicationRunner → CommandLineRunner

实现方法

1. 实现ServletContextListener接口contextInitialized方法

代码如下(示例):

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Slf4j
@Component
public class ServletContextListenerImpl implements ServletContextListener {
	/**
     * 静态代码块会在依赖注入后自动执行,并优先执行
     */
    static{
        log.info("启动时自动执行 静态代码块");
    }
    /**
     * 在初始化Web应用程序中的任何过滤器或Servlet之前,将通知所有ServletContextListener上下文初始化。
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("启动时自动执行 ServletContextListener 的 contextInitialized 方法");
    }
}

注意:该方法会在填充完普通Bean的属性,但是还没有进行Bean的初始化之前执行

关于在 contextInitialized 中使用其他 Bean 的问题

答案是:不能直接使用。原因如下:

  1. 生命周期顺序

    • ServletContextListener 是由 Servlet 容器(如 Tomcat)管理的,它在 Servlet 容器启动时被初始化。
    • Spring 容器的初始化是在 Servlet 容器初始化之后进行的。在 contextInitialized 方法被调用时,Spring 的 Bean 容器可能尚未完全初始化,因此无法保证其他 Bean 已经被创建并注入到当前类中。
  2. 依赖注入机制

    • Spring 的依赖注入(DI)机制是在 Spring 容器启动后才生效的。在 contextInitialized 方法中,Spring 的 Bean 容器尚未完全启动,因此无法通过依赖注入的方式获取其他 Bean。

2.静态代码块方式

将要执行的方法所在的类交给Spring容器扫描(@Component),在类中添加静态代码块,这样在Spring在扫描这类时候就会自动执行静态代码,达到代码自动运行的效果。示例代码见上面第1种方法的代码。

3. @PostConstruct注解方式

将要执行的方法所在的类交给Spring容器扫描(@Component),并且在要执行的方法上添加@PostConstruct注解执行
代码如下(示例):

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class PostConstructTest {
    @PostConstruct
    public void postConstruct() {
        log.info("启动时自动执行  @PostConstruct 注解方法");
    }
}

如果是某个service,需要执行init方法,而且需要使用依赖注入的对象,就可以使用如下的代码示例。

@Service
public class YourService {
    @Service XXService xxservice;  // 可以使用注入的其他service等交给Spring容器管理的对象。

    @PostConstruct
    public void init() {
        // 当前service 的初始化代码或者缓存代码
        
        // xxservice.xx();
    }
}

4. 实现ServletContextAware接口setServletContext 方法

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

@Slf4j
@Component
public class ServletContextAwareImpl implements ServletContextAware {
    /**
     * 在填充普通bean属性之后但在初始化之前调用
     * 类似于InitializingBean's 的 afterPropertiesSet 或自定义init方法的回调
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        log.info("启动时自动执行 ServletContextAware 的 setServletContext 方法");
    }
}

5. @EventListener方式

将要执行的方法所在的类交给个Spring容器扫描(@Component),并且在要执行的方法上添加@EventListener注解执行

代码如下(示例):

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class EventListenerTest {
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("启动时自动执行  @EventListener 注解方法");
    }
}

6. 实现ApplicationRunner接口run 方法

代码如下(示例):

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Set;

@Slf4j
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    /**
     * 用于指示bean包含在SpringApplication中时应运行的接口。可以定义多个ApplicationRunner bean
     * 在同一应用程序上下文中,可以使用有序接口或@order注释对其进行排序。
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("启动时自动执行 ApplicationRunner 的 run 方法");

        Set<String> optionNames = args.getOptionNames();
        for (String optionName : optionNames) {
            log.info("这是传过来的参数[{}]", optionName);
        }
        String[] sourceArgs = args.getSourceArgs();
        for (String sourceArg : sourceArgs) {
            log.info("这是传过来sourceArgs[{}]", sourceArg);
        }
    }
}

7. 实现CommandLineRunner接口run 方法

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        log.info("启动时自动执行 CommandLineRunner 的 run 方法");
    }
}

以上方式执行顺序

image.png

参考

介绍Spring Boot 启动时,自动执行指定方法的 7 种方法