Home > Java > javaTutorial > What are the execution methods after the springboot project is started?

What are the execution methods after the springboot project is started?

王林
Release: 2023-05-27 23:16:46
forward
2051 people have browsed it

1 Method

  • ApplicationListener< ContextRefreshedEvent> Not recommended

  • ApplicationListener Recommended

  • CommandLineRunner Recommended

Method 1: spring's ApplicationListenerinterface

Implement the ApplicationListener interface and implement the onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) method

@Service
public class SearchReceive implements  ApplicationListener<ContextRefreshedEvent> {
   @Override
   public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
       if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次
           //需要执行的方法
       }
   }
}
Copy after login

Method 2 :springboot's ApplicationRunner interface

ApplicationListener and CommandLineRunner interfaces are provided by springBoot to execute specified methods after the spring container is loaded. The main difference between the two interfaces is the input parameters.

Implement ApplicationRunner interface

@Component
@Order(value = 1)
public class AfterRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("执行方法");
    }
}
Copy after login

Method 3: springboot’s CommandLineRunner interface

Implement CommandLineRunner interface

@Component
@Order(value = 2)
public class CommandLineRunnerImpl implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("执行方法");
    }
}
Copy after login

Note: If you implement both ApplicationListener and CommandLineRunner interfaces at the same time , the methods of the ApplicationRunner interface are executed first, and then the CommandLineRunner;

@Slf4j
@Component
public class RunnerTest implements ApplicationRunner, CommandLineRunner {
 
  @Override
  public void run(ApplicationArguments args) throws Exception {
    System.out.println("服务启动RunnerTest   ApplicationRunner执行启动加载任务...");
  }
 
  @Override
  public void run(String... args) throws Exception {
    System.out.println("服务启动RunnerTest    CommandLineRunner 执行启动加载任务...");
    }
  }
}
Copy after login

2 Specify the execution order

When both the ApplicationRunner and CommondLineRunner interfaces are implemented in the project, you can use the Order annotation or implement the Ordered interface. Specify the execution order, the smaller the value, the first to execute.

3 Principle

The run method of SpringApplication will execute the afterRefresh method.

The afterRefresh method will execute the callRunners method.

The callRunners method will call all methods that implement the ApplicationRunner and CommondLineRunner interfaces.

The above is the detailed content of What are the execution methods after the springboot project is started?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template