Spring MVC 应用程序中的 CommandLineRunner 功能

CommandLineRunner functionality in Spring MVC application

我们有一个包含多个 Maven 模块的 Spring MVC 项目。我们将其打包成一个 EAR 并将其部署到 WildFly 服务器。

我正在尝试在项目启动时做一份兼职工作。因此,我想到了 CommandLineRunner 接口,项目会编译 运行 但 commandLineRunner 运行 方法不会 运行.

我想这是因为我们使用的是 MVC Spring 项目,而不是 SpringBoot 项目,它有自己的嵌入式服务器。 您能否建议在 Spring MVC 中实现此类概念的任何方法?

谢谢。

你可以这样做:

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class StartupExec implements {

  @EventListener(ContextRefreshedEvent.class)
  public void contextRefreshedEvent() {
    // do whatever you need here 
  }
}

这是来自this answer.