In this chapter, we added the ability to initialize resources at project startup. If you have any questions, please contact me at [email protected]. Ask for directions of various gods, thank you

In our actual work, there are always such requirements that some initialization operations need to be done when the project is started, such as reading configuration file information, database connection, clearing cache information, etc. Spring Boot provides us with two interfaces to help us implement this requirement. These interfaces are CommandLineRunner and ApplicationRunner, which are executed when the container is started.

One: Common ground and difference

Common: first, the execution time is always executed when the container has been started. Second, both interfaces have a run() method.

Differences: The run method in ApplicationRunner takes ApplicationArguments, while the Run method in CommandLineRunner takes a String array.

Two: Use ApplicationRunner to fulfill requirements

To create the core – startuprunner – StartupRunner1

package com.example.demo.core.startuprunner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value = 1)
public class StartupRunner1 implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(StartupRunner1.class);

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        logger.info("Server started successfully! <<<< Using the ApplicationRunner interface"); }}Copy the code

Note: @Component needs to be added otherwise SpringBoot will not scan the class and will not execute it.

Start the server and we can see



Three: Use CommandLineRunner to fulfill requirements

To create the core – startuprunner – StartupRunner2

package com.example.demo.core.startuprunner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value = -1)
public class StartupRunner2 implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(StartupRunner2.class);

    @Override
    public void run(String... args) throws Exception{
        logger.info("Server started successfully! <<<< using the CommandLineRunner interface"); }}Copy the code



We can see that the output is successful and CommandLineRunner is executed first in ApplicationRunner

Use the Order annotation to modify the execution Order

What if there are multiple implementation classes and we need to execute them in a certain order

Solution: Annotate the implementation class with @Order to specify the Order of execution

Note: The smaller the number, the higher the priority, i.e. the class annotated by @order (1) will be executed before the class annotated by @order (2).

The project address

Code cloud address: gitee.com/beany/mySpr…

GitHub address: github.com/MyBeany/myS…

Writing articles is not easy, if it is helpful to you, please help click star

At the end

The function of initializing resources at project startup has been completed, and the subsequent functions will be updated successively. If you have any questions, please contact me at [email protected]. Ask for directions from various gods, thank you.