2021年9月2日 星期四

紀錄一下自己用 Spring Boot 是如何建立一個控制台應用程式(命令列工具/console application)

如題,最近因為工作需求,要建立一個控制台應用程式

基於環境已經有安裝 Java 了,所以沒用自己愛用的 go 來寫。

既然要用 Java ,為求方便,所以用 Spring Boot。沒想到用 Spring Boot 寫控制台應用程式的教學資源這麼少。所以在此紀錄,

建立專案

首先自然是建立 Spring Boot 專案。

如果是用 IDE 的話, Eclipse 或是 IntelliJ 應該都有插建協助安裝(至少我用的 Eclipse 是有啦)

如果沒插件或是沒用 IDE ,也能到 https://start.spring.io/ 建立範例專案,專案的資料夾架構大致如下

│  .classpath
│  .gitignore
│  .project
│  build.gradle
│  gradlew
│  gradlew.bat
│  settings.gradle
│
├─.gradle
├─.settings
├─bin
├─gradle
│  └─wrapper
│          gradle-wrapper.jar
│          gradle-wrapper.properties
│
└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─example
    │  │          └─demo
    │  │                DemoApplication.java
    │  └─resources
    │         application.properties
    └─test

這邊注意 build.gradle ,應該要是

implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

而不應該是

implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

如果是後者,DemoApplication.java 執行 SpringApplication.run(DemoApplication.class, args);時就會變成 Web APP 而不是我們要的 Console APP

資料庫連線

Spring Boot Web APP 本身已經內建資料庫連線功能了。但 Console APP 沒有。

需要用到資料庫連線的話,請在 build.gradle 追加下面個 library

implementation 'org.springframework.boot:spring-boot-starter-jdbc'

如果是 MySQL 的資料庫,還需要

implementation 'mysql:mysql-connector-java'

其他資料庫同理。

Multiple Runner

如果你的程式需要執行多樣功能

例如你的程式是 Mediawiki 機器人,會自動檢查所有多重重定向並將之修正、或者自動替某種格式標題的條目建立重定向(例如替 Smith White 建立 Smith W. 的重定向)。

那你的動作會有

  1. 登入維基
  2. 自動檢查所有多重重定向並修正
  3. 自動建立重定向

那你可以建立 Runner ,將三件事分別建立三個不同的 Runner,相關教學可參閱Tutorials Point 的 «Spring Boot - Runners»

以上述為例,則建立下面三個 class

@Component
@Order(1)
public class LoginRunner implements ApplicationRunner {
	@Autowired
	Object cache; // 儲存 Cache 用的 Object ,基於這個物件到底是什麼類型不是重點,所以這邊直接使用 Object

	@Override
	public void run(ApplicationArguments arg0) throws Exception {
		// 進行登入
		// 將 Token 存到 cache object
	}
}
@Component
@Order(2)
public class CheckRedirectRunner implements ApplicationRunner {
	@Autowired
	Object cache;

	@Override
	public void run(ApplicationArguments arg0) throws Exception {
		// 從 Cache 讀 Token 
		// 搜尋所有重定向,找出所有有問題的
		// 修改有問題重定向
	}
}
@Component
@Order(3)
public class AutoCreateRedirectRunner implements ApplicationRunner {
	@Autowired
	Object cache;
	
	@Override
	public void run(ApplicationArguments arg0) throws Exception {
		// 從 Cache 讀 Token 
		// 搜尋所有條目
		// 根據條件,建立重定向
	}
}

這樣可以讓每個 class 只需處理自己的任務,也不用全部擠到 main 之中

這邊都是用 ApplicationRunner,但 runner 其實還有 CommandLineRunner。兩者的差異網上有很多資訊,這邊就不贅述。

Runner 順序

這邊注意一點,上面的 class 都有使用 @Order annotation。

這是用來指定每個 runner 的執行順序。

像是上面的例子,你要得到 token 也要先登入才行,所以 order 1

其餘兩個之間順序沒差,但一定要比 LoginRunner 後,所以都是 > 1(當然不一定要是 2 或 3。想要 100 之類的數字也是可以)

結語

總之先記錄建立 Spring Boot Console APP 相關的資訊

至於 log 設定、gradle 設定、如何建立 Bean ……等等,這些 Spring 通用知識這邊都跳過,在這純粹只紀錄 Spring Boot Console APP 相關的而已。

沒有留言:

張貼留言