天天看點

Spring Boot非web應用程式執行個體(十三)

這篇博文主要用于新手學習Spring Boot,同時也記錄自己學習的過程…

文章内容主要來源于易百教程

在Spring Boot中,要建立一個非Web應用程式,實作

CommandLineRunner

并覆寫run()方法,例如:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@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.項目結構

一個标準的Maven項目結構。如下所示-

Spring Boot非web應用程式執行個體(十三)

2.項目依賴

隻有依賴

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.th</groupId>
    <artifactId>spring-boot-non-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-non-web</name>
    <url>http://maven.apache.org</url>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

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

    <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

傳回消息的服務,如下 HelloMessageService.java 代碼所示 -

package com.th.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 如下所示 -

下面是CommandLineRunner示例,如果運作這個Spring Boot,那麼run方法将是入口點。

SpringBootConsoleApplication.java 代碼内容如下所示 –

package com.th;

import static java.lang.System.exit;
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 com.th.service.HelloMessageService;

@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 > ) {
            System.out.println(helloService.getMessage(args[].toString()));
        } else {
            System.out.println(helloService.getMessage());
        }

        exit();
    }
}
           

4.執行個體運作示範

打包上面的項目并運作它,如下指令-

D:\workspace\spring-boot-non-web>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-non-web -SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ spring-boot-non-we
b ---
[INFO] Using 'UTF-' encoding to copy filtered resources.
[INFO] Copying  resource
[INFO] Copying  resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ spring-boot-non-web ---

[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ spring-boo
t-non-web ---
[INFO] Using 'UTF-' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\spring-boot-non-web\src\test\resou
rces
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ spring-boot-non
-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ spring-boot-non-web ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ spring-boot-non-web ---
[INFO] Building jar: D:\workspace\spring-boot-non-web\target\spring-boot-non-web--SNA
PSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ spring-boot-non-we
b ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  s
[INFO] Finished at: --T15::+:
[INFO] Final Memory: M/M
[INFO] ------------------------------------------------------------------------

D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web--SNAPSHOT.jar
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : Starting SpringBootConsoleApplication v0-SNAPSHOT on SZ10PD0703 with PID 
 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web--SNAPSHOT.jar started
by nicoletang in D:\workspace\spring-boot-non-web)
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : No active profile set, falling back to default profiles: default
-- ::  INFO  --- [           main] s.c.a.AnnotationConfigApplication
Context : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCon
text@db0f6b2: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [           main] o.s.j.e.a.AnnotationMBeanExporter
        : Registering beans for JMX exposure on startup
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : Started SpringBootConsoleApplication in  seconds (JVM running for )
Hello nicole
-- ::  INFO  --- [       Thread-2] s.c.a.AnnotationConfigApplication
Context : Closing org.springframework.context.annotation.AnnotationConfigApplicationContex
[email protected]: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter
        : Unregistering JMX-exposed beans on shutdown

D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web--SNAPSHOT.jar "
11"
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicat
ion      : Starting SpringBootConsoleApplication v0-SNAPSHOT on SZ10PD0703 with PID 
 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web--SNAPSHOT.jar starte
d by nicoletang in D:\workspace\spring-boot-non-web)
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicat
ion      : No active profile set, falling back to default profiles: default
-- ::  INFO  --- [           main] s.c.a.AnnotationConfigApplicatio
nContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCo
[email protected]: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [           main] o.s.j.e.a.AnnotationMBeanExporte
r        : Registering beans for JMX exposure on startup
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicat
ion      : Started SpringBootConsoleApplication in  seconds (JVM running for )
Hello 
-- ::  INFO  --- [       Thread-2] s.c.a.AnnotationConfigApplicatio
nContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationConte
[email protected]: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporte
r        : Unregistering JMX-exposed beans on shutdown

D:\workspace\spring-boot-non-web>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-non-web -SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-non-web
---
[INFO] Using 'UTF-' encoding to copy filtered resources.
[INFO] Copying  resource
[INFO] Copying  resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-non-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-boot-
non-web ---
[INFO] Using 'UTF-' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\spring-boot-non-web\src\test\resou
rces
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-boot-non-w
eb ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-boot-non-web ---
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ spring-boot-non-web ---
[INFO] Building jar: D:\workspace\spring-boot-non-web\target\spring-boot-non-web--SNA
PSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.1.RELEASE:repackage (default) @ spring-boot-non-we
b ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  s
[INFO] Finished at: --T15::+:
[INFO] Final Memory: M/M
[INFO] ------------------------------------------------------------------------

D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web--SNAPSHOT.jar
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : Starting SpringBootConsoleApplication v0-SNAPSHOT on SZ10PD0703 with PID 
 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web--SNAPSHOT.jar started
by nicoletang in D:\workspace\spring-boot-non-web)
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : No active profile set, falling back to default profiles: default
-- ::  INFO  --- [           main] s.c.a.AnnotationConfigApplication
Context : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCon
text@d8cfe0: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [           main] o.s.j.e.a.AnnotationMBeanExporter
        : Registering beans for JMX exposure on startup
Hello nicole
-- ::  INFO  --- [       Thread-2] s.c.a.AnnotationConfigApplication
Context : Closing org.springframework.context.annotation.AnnotationConfigApplicationContex
[email protected]: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter
        : Unregistering JMX-exposed beans on shutdown

D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web--SNAPSHOT.jar "
Miss"
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : Starting SpringBootConsoleApplication v0-SNAPSHOT on SZ10PD0703 with PID 
 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web--SNAPSHOT.jar started
by nicoletang in D:\workspace\spring-boot-non-web)
-- ::  INFO  --- [           main] com.th.SpringBootConsoleApplicati
on      : No active profile set, falling back to default profiles: default
-- ::  INFO  --- [           main] s.c.a.AnnotationConfigApplication
Context : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCon
text@d8cfe0: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [           main] o.s.j.e.a.AnnotationMBeanExporter
        : Registering beans for JMX exposure on startup
Hello Miss
-- ::  INFO  --- [       Thread-2] s.c.a.AnnotationConfigApplication
Context : Closing org.springframework.context.annotation.AnnotationConfigApplicationContex
[email protected]: startup date [Wed Aug  :: CST ]; root of context hierarchy
-- ::  INFO  --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter
        : Unregistering JMX-exposed beans on shutdown

D:\workspace\spring-boot-non-web>
           

相關文章:

  • Spring Boot教程(一)
  • Spring Boot是什麼?(二)
  • Spring Boot主要目标(三)
  • 新項目為什麼需要Spring Boot?(四)
  • Spring Boot核心和限制(五)
  • Spring Boot優點和缺點(六)
  • Spring Boot入門(七)
  • Spring Boot安裝(八)
  • Spring Boot應用程式開發入門(九)
  • Spring Boot JSP應用執行個體(十)
  • Spring Boot将WAR檔案部署到Tomcat(十一)
  • Spring Boot Hello World(Thymeleaf)示例(十二)
  • Spring Boot @ConfigurationProperties執行個體(十四)
  • Spring Boot SLF4J日志執行個體(十五)
  • Spring Boot Ajax執行個體(十六)

繼續閱讀