【问题标题】:How to start a thread on ApplicationContext load in Spring MVC application?如何在 Spring MVC 应用程序中启动 ApplicationContext 加载线程?
【发布时间】:2022-01-17 12:33:42
【问题描述】:

我正在创建这个 Spring Web MVC 应用程序。我想在 ApplicationContext 开始加载时启动一个线程。我该怎么做??

【问题讨论】:

  • 你没有。您不应该自己启动线程,通常如果您这样做,您就会以错误的方式做事。您尝试使用附加线程解决的问题是什么?

标签: spring multithreading hibernate spring-mvc spring-tool-suite


【解决方案1】:

我发现了这个及其工作

@Component
public class StartupApplicationListenerExample implements 
  ApplicationListener<ContextRefreshedEvent> {

    private static final Logger LOG 
      = Logger.getLogger(StartupApplicationListenerExample.class);

    public static int counter;

    @Override public void onApplicationEvent(ContextRefreshedEvent event) {
        LOG.info("Increment counter");
        counter++;
    }
}

【讨论】:

    猜你喜欢
    • 2020-08-23
    • 2016-02-20
    • 2014-01-16
    • 2011-01-17
    • 2021-09-13
    • 2015-03-21
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多