【问题标题】:CommandLineRunner functionality in Spring MVC applicationSpring MVC 应用程序中的 CommandLineRunner 功能
【发布时间】:2020-06-03 08:26:29
【问题描述】:

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

我正在尝试在项目启动时做一个单一的时间工作。于是想到了CommandLineRunner接口,项目可以编译运行,但是commandLineRunner的run方法不运行。

我猜这是因为我们使用的是 MVC Spring 项目,而不是带有自己的嵌入式服务器的 SpringBoot 项目。 你能建议在 Spring MVC 中实现这样一个概念的任何方法吗?

谢谢。

【问题讨论】:

标签: java spring spring-boot spring-mvc wildfly


【解决方案1】:

你可以这样做:

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

【讨论】:

  • 谢谢!尖锐的答案奏效了。不是您提到的部分,而是它的更新版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2015-10-27
  • 2016-10-08
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多