Spring Boot non-web application example

摘要: In Spring Boot, to create a non-web application, implements CommandLineRunner and override the run method, for example :

In Spring Boot, to create a non-web application, implements CommandLineRunner and override the run method, for example :

import org.springframework.boot.CommandLineRunner;
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConsoleApplication.class, args);
    //access command line arguments
    @Override
    public void run(String... args) throws Exception {
        //do something

1. Project Structure

A standard Maven project structure.

2. Project Dependency

Only spring-boot-starter

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong</groupId>
    <artifactId>spring-boot-simple</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. Spring

3.1 A service to return a message.

HelloMessageService.java
package com.mkyong.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class HelloMessageService {
    @Value("${name:unknown}")
    private String name;
    public String getMessage() {
        return getMessage(name);
    public String getMessage(String name) {
        return "Hello " + name;
application.properties
name=mkyong

3.2 CommandLineRunner example. If you run this Spring Boot, the run method will be the entry point.

SpringBootConsoleApplication.java
package com.mkyong;
import com.mkyong.service.HelloMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import static java.lang.System.exit;
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
    @Autowired
    private HelloMessageService helloService;
    public static void main(String[] args) throws Exception {
        //disabled banner, don't want to see the spring logo
        SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
	// Put your logic here.
    @Override
    public void run(String... args) throws Exception {
        if (args.length > 0) {
            System.out.println(helloService.getMessage(args[0].toString()));
        } else {
            System.out.println(helloService.getMessage());
        exit(0);

4. DEMO

Package and run it.

## Go to project directory
## package it
$ mvn package
$ java -jar target/spring-boot-simple-1.0.jar
Hello mkyong
$ java -jar target/spring-boot-simple-1.0.jar "donald trump"
Hello donald trump

References

  1. CommandLineRunner JavaDoc

上一篇: MySQL Establishing SSL connection without servers identity verification is not recommended
下一篇: Spring Boot JDBC + MySQL + HikariCP example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号