SpringBoot的ApplicationRunner

There may be scenarios in development. You need to do something when the vessel started. For example, read the configuration files, database connections or the like. SpringBoot provides us with two interfaces to help us achieve this demand. These two interfaces are CommandLineRunner and ApplicationRunner. Their execution time for the start of the container when finished.

Both interfaces have a run method, we only need to implement this method. This differs from the two interfaces in that: the parameters in the run method is ApplicationRunner ApplicationArguments, the parameter CommandLineRunner interface array String run method. I am currently in used in the project is ApplicationRunner. It is so realized:

package com.jdddemo.demo.controller;
 
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
@Component
public class JDDRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(args);
        System.out.println("这个是测试ApplicationRunner接口");
    }
}

 

 Execution results are as follows:

 

It can be used to add the cache, the timing task, certain business logic may be implemented (@Autowired can inject the JavaBean)


---------------------
Author: Jiang small gardenia in North
Source: CSDN
Original: https: //blog.csdn.net/jdd92/article/details/81053404

Guess you like

Origin www.cnblogs.com/ryelqy/p/11081752.html