[SpringBoot]容器启动时,自动执行自定义方法

一、为什么

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

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

检查当前项目运行环境;

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

执行某个特定方法;

二、怎么做

1.实现ServletContextListener接口contextInitialized方法

@Slf4j

@Component

public class ServletContextListenerImpl implements ServletContextListener {

    /**

    * 在初始化Web应用程序中的任何过滤器或Servlet之前,将通知所有ServletContextListener上下文初始化。

    */

    @Override

    public void contextInitialized(ServletContextEvent sce) {

        log.info("启动时自动执行 ServletContextListener 的 contextInitialized 方法");

    }

}

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

2.静态代码块方式

@Component

public class ServletContextListenerImpl {

/**

    * 静态代码块会在依赖注入后自动执行,并优先执行

    */

    static{

        log.info("启动时自动执行 静态代码块");

    }

}

3.@PostConstruct注解方式

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

@Slf4j

@Component

public class PostConstructTest {

    @PostConstruct

    public void postConstruct() {

        log.info("启动时自动执行  @PostConstruct 注解方法");

    }

}

4.实现ServletContextAware接口setServletContext 方法

@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注解执行

@Slf4j

@Component

public class EventListenerTest {

    @EventListener

    public void onApplicationEvent(ContextRefreshedEvent event) {

        log.info("启动时自动执行  @EventListener 注解方法");

    }

}

6.实现ApplicationRunner接口run 方法

@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 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 方法

@Slf4j

@Component

public class CommandLineRunnerImpl implements CommandLineRunner {

    @Override

    public void run(String... args) throws Exception {

        log.info("启动时自动执行 CommandLineRunner 的 run 方法");

    }

}

三、以上方式执行顺序

。。。

初始化Root ApplicationContext

启动时自动执行 静态代码块

启动时自动执行 ServletContextListener 的 contextInitialized 方法

启动时自动执行  @PostConstruct 注解方法

启动时自动执行 ServletContextAware 的 setServletContext 方法

初始化线程池

Tomcat启动

启动时自动执行  @EventListener 注解方法

Application启动成功

启动时自动执行 ApplicationRunner 的 run 方法

启动时自动执行 CommandLineRunner 的 run 方法

你可能感兴趣的:([SpringBoot]容器启动时,自动执行自定义方法)