Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

バッチ的なアプリをつくるときにこの方法を使ってみました。
簡単な仕様としては以下のようなものです
・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

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

natty420
furyu
プリ機やWEBサービス、ゲームなど様々なジャンルの開発をしています。人々のこころを豊かで幸せにする良質なエンタテインメントを創出していきます!
http://www.furyu.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした