Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

1. ApplicationRunner

An interface defined in the org.springFramework. boot package is used by defining classes that implement the ApplicationRunner interface and implement the Run () method in the interface.

Note that the implementation class must be injected into the Spring container, otherwise SpringBoot will not automatically execute the run() method in that class when it starts.

1.1 Interface Definition

@FunctionalInterface
public interface ApplicationRunner {
    void run(ApplicationArguments args) throws Exception;
}
Copy the code

1.2 Interface implementation classes

@Component
public class AutoRunApplicationRunner implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(AutoRunApplicationRunner.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("Implement ApplicationRunner's run method at {}".newDate()); }}Copy the code

1.3 Execution Results:

2. CommandLineRunner

The same interface defined in the org.springFramework. boot package needs to define classes that implement the ApplicationRunner interface and implement the Run () method in the interface. The implementation class must be injected into the container, otherwise SpringBoot cannot automatically execute the run() method in that class.

2.1 Interface Definition

@FunctionalInterface
public interface CommandLineRunner {
    void run(String... args) throws Exception;
}
Copy the code

2.2 Interface implementation classes

@Component
public class AutoRunCommandLineRunner implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(AutoRunCommandLineRunner.class);
    @Override
    public void run(String... args) throws Exception {
        log.info("Implement AutoRunCommandLineRunner run method automatic execution, time: {}, parameter content:".newDate(),args); }}Copy the code

2.3 Execution Results

3. ApplicationRunner and CommandLineRunner

3.1 the difference between

  1. In normal use, the Run () method of the Implementation class of the ApplicationRunner interface is executed before the Run () method of the implementation class of CommandLineRunner is executed. The Run () method of the CommandLineRunner implementation class is not executed until the Run () method of the ApplicationRunner implementation class has been executed.
  2. The run() methods on both interfaces are parameter methods. The difference is that ApplicationRunner corresponds to an ApplicationArguments object, while CommandLineRunner takes a String.

The following parameters are passed in at project startup

The result information of the execution can be obtained:

3.2 @ sort Order

While ApplicationRunner and CommandLineRunner are not absolute, we can use the @Order annotation to specify the execution Order of the two implementation classes.

@ Order annotation is the spring framework of org. Springframework. Core. The annotation package provides annotations, detailed information:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Documented
public @interface Order {
    int value(a) default 2147483647;
}
Copy the code

We use the annotation @ORDER (value = 1) for CommandLineRunner’s implementation class and @Order(value = 2) for ApplicationRunner’s implementation class to obtain the specified execution Order

4. @PostContstruct

The @PostContstruct annotation is an annotation content provided in Java’s own Javax. Annotation package that decorates a non-static method and executes the annotated method after the class is created.

In the Spring framework, the order of method execution to annotate this annotation is executed after constructor and property injection, that is, after class loading.

4.1 Definition of Annotations

 @Documented
 @Retention (RUNTIME)
 @Target(METHOD)
 public @interface PostConstruct {
 }
Copy the code

4.2 Usage

If you want to implement automatic method execution at project startup, you need to hand over the defined classes to the Spring container for management.

@Component
public class AutoRunUserAnnotation {

    private static final Logger log = LoggerFactory.getLogger(AutoRunUserAnnotation.class);

    @PostConstruct
    public void init(a){
        log.info("Method is executed automatically using the @postconstruct annotation at {}".newDate()); }}Copy the code

4.3 Execution Result

In the Spring framework, if you define a class that uses a container class annotation, Spring loads all classes that use the annotation when the container is first instantiated, and immediately executes the @PostContstruct annotation in the class when the container is loaded.

The current annotation annotation method is executed after the container is added and before the project is finally started. The result is shown below: