简体   繁体   中英

CommandLineRunner functionality in Spring MVC application

We have a Spring MVC project with multiple Maven modules. We package it into an EAR and deploy it to a WildFly server.

I am trying to do a single time job on project start up. Thus, I thought about the CommandLineRunner interface, the project would compile and run but the commandLineRunner run method wouldn't run.

I guess it's because we are using an MVC Spring project and not a SpringBoot one with its own embedded server. Can you suggest any ways to implement such a concept in Spring MVC ?

Thanks.

You can do something like that:

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 is from this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM