官术网_书友最值得收藏!

  • Spring Boot Cookbook
  • Alex Antonov
  • 453字
  • 2021-07-16 19:44:52

Using the command-line runners

With our basic application skeleton ready, let's add some meat to the bones by making our application do something.

Let's start by first creating a class named StartupRunner. This will implement the CommandLineRunner interface, which basically provides just one method—public void run(String… args)—that will get called by Spring Boot only once after the application has started.

How to do it…

  1. Create the file named StartupRunner.java under the src/main/java/org/test/bookpub/ directory from the root of our project with the following content:
    package org.test.bookpub;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.boot.CommandLineRunner;
    public class StartupRunner implements CommandLineRunner {
      protected final Log logger = LogFactory.getLog(getClass());
      @Override
      public void run(String... args) throws Exception {
        logger.info("Hello");
      }
    }
  2. After we have defined the class, let's proceed by defining it as @Bean in the BookPubApplication.java application configuration, which is located in the same folder as our newly created StartupRunner.java, shown as follows:
    @Bean
    public StartupRunner schedulerRunner() {
      return new StartupRunner();
    }

How it works…

If we run our application again by executing $ ./gradlew clean bootRun, we will get an output that is similar to our previous application startup. However, we will see our Hello message in the logs as well, which will look as follows:

2015-03-10 21:57:51.048 INFO --- org.test.bookpub.StartupRunner : Hello

Even though the program will get terminated on execution, at least we made it do something!

Command line runners are a useful functionality to execute the various types of code that only have to be run once, right after application startup. Some may also use this as a place to start various executor threads but Spring Boot provides a better solution for this task, which will be discussed at the end of this chapter. The CommandLineRunner interface is used by Spring Boot to scan all of its implementations and invoke each instance's run method with the startup arguments. We can also use an @Order annotation or implement an Ordered interface so as to define the exact order in which we want Spring Boot to execute them. For example, Spring Batch relies on the runners in order to trigger the execution of the jobs.

As command-line runners are instantiated and executed after the application has started, we can use the dependency injection to our advantage in order to wire in whatever dependencies that we need, such as data sources, services, and other components. These can be utilized later while implementing run(String... args) method..

Note

It is important to note that if any exceptions are thrown inside the run(String… args) method, this will cause the context to close and an application to shut down. Wrapping the risky code blocks with try/catch is recommended in order to prevent this from happening.

主站蜘蛛池模板: 平昌县| 花莲市| 奉贤区| 台北县| 尼木县| 丹巴县| 钦州市| 泾阳县| 秦皇岛市| 宿松县| 隆昌县| 天门市| 清远市| 铜鼓县| 乌拉特后旗| 禹州市| 农安县| 邵阳县| 英德市| 和林格尔县| 洞头县| 竹北市| 鄂州市| 松阳县| 巴马| 江门市| 金华市| 盐津县| 博野县| 博白县| 香河县| 正阳县| 山东省| 错那县| 沛县| 绥阳县| 泊头市| 苍南县| 永州市| 安岳县| 黔江区|