SpringBootの非Webアプリケーションの例
Spring Boot Non Web Application Example
Spring Bootで、非Webアプリケーションを作成するには、実装CommandLineRunner
そして上書きrun()
メソッドを次に示します。
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 } }
Java 1.プロジェクトの構造
標準のMavenプロジェクト構造。次のように -
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.0modelVersion> <groupId>com.yiibaigroupId> <artifactId>spring-boot-non-webartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>spring-boot-non-webname> <url>http://maven.apache.orgurl> <properties> <java.version>1.8java.version> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> dependencies> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>1.5.1.RELEASEversion> parent> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
XML 3.春
メッセージを返すサービスは次のとおりです。HelloMessageService.javaコードは示しています-
package com.yiibai.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 } }
Java プロパティファイル構成ファイル:application.properties次のように -
name=yiibai
シェル 以下はCommandLineRunner
例です。このSpringBootを実行すると、run
メソッドがエントリポイントになります。
SpringBootConsoleApplication.javaコードの内容は次のとおりです-
package com.yiibai import com.yiibai.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) } }
Java 4.デモを実行します
上記のプロジェクトをパッケージ化して実行するには、次のコマンドを実行します-
## Go to project directory ## package it $ mvn package $ java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar Hello yiibai $ java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar 'Max su' Hello Max su