SpringBoot-ApplicationRunner, CommandLineRunner(스프링부트 구동시 동작시킬 코드실행)

들어가기

Spring boot 어플리케이션을 시작시키면서 동시에 동작시킬 코드가 있다면, ApplicationRunner와 CommandLineRunner 인터페이스를 구현한 bean을 사용하면 된다.

사용법 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package hanumoka.portfolio.nao;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Component
public class TestApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("============ApplicationRunner");
}
}


@Order(2)
@Component
class TestApplicationRunner2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("============ApplicationRunner2");
}
}

@Order(1)
@Component
class TestCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("============CommandLineRunner1");
}
}

두 인터페이스 모두 함수형 인터페이스이며, 구현한 run메소드의 파라미터인 args를 통해서 springboot어플리케이션 실행시 전달받은 파라미터(Program arguemnts) 값을 읽어올수 있다. VM Opions값은 읽어 올수 없다.

실행결과

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)

2019-01-04 15:38:24.445 INFO 14840 --- [ main] hanumoka.portfolio.nao.NaoApplication : Starting NaoApplication on DESKTOP-EU9BLID with PID 14840 (C:\Users\amagr\Desktop\Project\nao\target\classes started by amagr in C:\Users\amagr\Desktop\Project\nao)
2019-01-04 15:38:24.449 INFO 14840 --- [ main] hanumoka.portfolio.nao.NaoApplication : No active profile set, falling back to default profiles: default
2019-01-04 15:38:25.519 WARN 14840 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2019-01-04 15:38:25.539 INFO 14840 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
2019-01-04 15:38:25.539 INFO 14840 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1043 ms
2019-01-04 15:38:25.729 INFO 14840 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-04 15:38:25.931 INFO 14840 --- [ main] org.xnio : XNIO version 3.3.8.Final
2019-01-04 15:38:25.940 INFO 14840 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
2019-01-04 15:38:25.988 INFO 14840 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 1290 (http) with context path ''
포트:1290
2019-01-04 15:38:25.991 INFO 14840 --- [ main] hanumoka.portfolio.nao.NaoApplication : Started NaoApplication in 1.854 seconds (JVM running for 2.285)
============ApplicationRunner
============CommandLineRunner1
============ApplicationRunner2

springboot 구동시 시작된 뒤, ApplicationRunner, CommandLineRunner 인터페이스로 구현한 run메소드가 동작하는 것을 확인 할 수 있다. 위 처럼, Order어노테이션으로 이들간의 동작 순서를 지정 할 수도 있다.

참고자료

https://dzone.com/articles/spring-boot-applicationrunner-and-commandlinerunne