Spring Boot Console Application – Spring Boot控制台应用程序

最后修改: 2018年 7月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll explore how to create a simple console-based application using Spring Boot.

在这个快速教程中,我们将探讨如何使用Spring Boot创建一个简单的基于控制台的应用程序。

2. Maven Dependencies

2.Maven的依赖性

Our project relies on the spring-boot parent:

我们的项目依赖于spring-boot父体。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.0</version>
</parent>

The initial dependency required is:

需要的初始依赖性是。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

3. Console Application

3.控制台应用

Our console application consists of a single class, SpringBootConsoleApplication.java, which is the main class for out Spring Boot console application.

我们的控制台应用程序由一个单一的类组成,SpringBootConsoleApplication.java,它是Spring Boot控制台应用程序的主类。

We’re using Spring’s @SpringBootApplication annotation on our main class to enable auto-configuration.

我们在主类上使用Spring的@SpringBootApplication注解来启用自动配置。

This class also implements Spring’s CommandLineRunner interfaceCommandLineRunner is a simple Spring Boot interface with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded.

该类还实现了Spring的CommandLineRunner接口CommandLineRunner是一个简单的Spring Boot接口,有一个run方法。在应用上下文被加载后,Spring Boot将自动调用所有实现该接口的Bean的run方法。

Here’s our console application:

这里是我们的控制台应用程序。

@SpringBootApplication
public class SpringBootConsoleApplication 
  implements CommandLineRunner {

    private static Logger LOG = LoggerFactory
      .getLogger(SpringBootConsoleApplication.class);

    public static void main(String[] args) {
        LOG.info("STARTING THE APPLICATION");
        SpringApplication.run(SpringBootConsoleApplication.class, args);
        LOG.info("APPLICATION FINISHED");
    }
 
    @Override
    public void run(String... args) {
        LOG.info("EXECUTING : command line runner");
 
        for (int i = 0; i < args.length; ++i) {
            LOG.info("args[{}]: {}", i, args[i]);
        }
    }
}

We should also specify the spring.main.web-application-type=NONE Spring property. This property will explicitly inform Spring that this isn’t a web application.

我们还应该指定spring.main.web-application-type=NONE Spring属性。这个属性将明确告知Spring,这不是一个Web应用程序。

When we execute SpringBootConsoleApplication, we can see the following logged:

当我们执行SpringBootConsoleApplication时,我们可以看到以下记录。

00:48:51.888 [main] INFO  c.b.s.SpringBootConsoleApplication - STARTING THE APPLICATION
00:48:52.752 [main] INFO  c.b.s.SpringBootConsoleApplication - No active profile set, falling back to default profiles: default
00:48:52.851 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext 
  - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6497b078: startup date [Sat Jun 16 00:48:52 IST 2018]; root of context hierarchy
00:48:53.832 [main] INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
00:48:53.854 [main] INFO  c.b.s.SpringBootConsoleApplication - EXECUTING : command line runner
00:48:53.854 [main] INFO  c.b.s.SpringBootConsoleApplication - args[0]: Hello World!
00:48:53.860 [main] INFO  c.b.s.SpringBootConsoleApplication - Started SpringBootConsoleApplication in 1.633 seconds (JVM running for 2.373)
00:48:53.860 [main] INFO  c.b.s.SpringBootConsoleApplication - APPLICATION FINISHED
00:48:53.868 [Thread-2] INFO  o.s.c.a.AnnotationConfigApplicationContext 
  - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6497b078: startup date [Sat Jun 16 00:48:52 IST 2018]; root of context hierarchy
00:48:53.870 [Thread-2] INFO  o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown

Note that the run method is called after the application context is loaded, but before the execution of the main method is complete.

请注意,run方法是在应用上下文被加载后,但在main方法的执行完成前被调用。

Most console applications will only have a single class that implements CommandLineRunner. If our application has multiple classes that implement CommandLineRunner, the order of execution can be specified using Spring’s @Order annotation.

大多数控制台应用程序只会有一个实现CommandLineRunner的类。如果我们的应用程序有多个实现CommandLineRunner的类,可以使用Spring的@Order注解来指定执行的顺序

4. Conclusion

4.总结

In this brief article, we learned how to create a simple console-based application using Spring Boot.

在这篇简短的文章中,我们学习了如何使用Spring Boot创建一个简单的基于控制台的应用程序。

The full source code of our examples is available over on GitHub.

我们的例子的完整源代码可以在GitHub上找到