13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Bootでコマンドラインアプリを作ってみた

Posted at

バッチ的なアプリをつくるときにこの方法を使ってみました。
簡単な仕様としては以下のようなものです
・30分に一回実行させる(rundeckを使用、rundeckから処理実行)
・あるデータを参照し、条件が整っていればある処理を実行(今回つくるもの)

プロジェクト作成

https://start.spring.io/
dependenciesは特になにも指定は不要です。

spring_initializr.JPG

ビルド、実行

このクラスが呼び出されます

package com.example.commandlineappli;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.example.commandlineappli.service.SampleService;

@SpringBootApplication
public class CommandlineappliApplication {

    
	public static void main(String[] args) {
		SpringApplication.run(CommandlineappliApplication.class, args);
	}
}

プロフェクト直下にて

gradle build
java -jar build\libs\commandlineappli-0.0.1-SNAPSHOT.jar 

起動しましたね!
基本はこれだけです。

クラスの呼び出し方

serviceパッケージを作成し、クラスを作ってみます

package com.example.commandlineappli.service;

import org.springframework.stereotype.Service;

@Service
public class SampleService {

    public String execute() {
        return "###########success###########";
    }
    
}

CommandlineappliApplication から呼び出す場合 @Autowired@Resourceをつかってもコンポーネントはセットされません。
以下のようにConfigurableApplicationContext からコンポーネントを取り出して呼び出します。

package com.example.commandlineappli;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.example.commandlineappli.service.SampleService;

@SpringBootApplication
public class CommandlineappliApplication {
    
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(CommandlineappliApplication.class, args);
        SampleService service = ctx.getBean(SampleService.class);
        System.out.println(service.execute());
    }
}

successが表示されましたね!


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.10.RELEASE)

2018-03-26 18:04:48.440  INFO 15544 --- [           main] c.e.c.CommandlineappliApplication        : Starting CommandlineappliApplication on PC1968 with PID 15544 (C:\home\develop\workspace\commandlineappli\build\libs\commandlineappli-0.0.1-SNAPSHOT.jar started by 0FR110017 in C:\home\develop\workspace\commandlineappli)
2018-03-26 18:04:48.445  INFO 15544 --- [           main] c.e.c.CommandlineappliApplication        : No active profile set, falling back to default profiles: default
2018-03-26 18:04:48.556  INFO 15544 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1698c449: startup date [Mon Mar 26 18:04:48 JST 2018]; root of context hierarchy
2018-03-26 18:04:49.388  INFO 15544 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-26 18:04:49.413  INFO 15544 --- [           main] c.e.c.CommandlineappliApplication        : Started CommandlineappliApplication in 1.549 seconds (JVM running for 2.144)
###########success###########
2018-03-26 18:04:49.418  INFO 15544 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1698c449: startup date [Mon Mar 26 18:04:48 JST 2018]; root of context hierarchy
2018-03-26 18:04:49.419  INFO 15544 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

これで実行するたびに起動し処理が終了するコマンドアプリの完成ですヽ(´エ`)ノ

13
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?