๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring/Spring Boot

์Šคํ”„๋ง ๋ถ€ํŠธ ์•ฑ์— ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

by Soono991 2023. 3. 19.

๐Ÿ’ก์ด ํฌ์ŠคํŒ…์€ ํ† ๋น„ ๋‹˜์˜ ์œ ํŠœ๋ธŒ ์˜์ƒ์ธ ์Šคํ”„๋ง ๋ถ€ํŠธ ์•ฑ์— ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€์„ ์‹œ์ฒญํ•˜๊ณ  ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค.

 
ํ† ๋น„ ๋‹˜์˜ ์˜์ƒ์„ ์‹œ์ฒญํ•˜๋ฉฐ ์ •๋ฆฌํ•œ GitHub Repository์ž…๋‹ˆ๋‹ค.

GitHub - kiekk/study-spring-boot-inject-initializer-code

Contribute to kiekk/study-spring-boot-inject-initializer-code development by creating an account on GitHub.

github.com

 

๋ฐ”๋กœ ๋ณธ๋ก ๋ถ€ํ„ฐ ๋“ค์–ด๊ฐ€๋ฉด ์Šคํ”„๋ง ๋ถ€ํŠธ ์•ฑ์— ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํฌ๊ฒŒ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • CommandLineRunner
  • ApplicationRunner
  • @EventListener

 

CommandLineRunner

๋จผ์ € CommandLineRunner interface๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

CommandLineRunner๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด run() ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, String [] ํ˜•ํƒœ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
์„ค๋ช…์„ ์กฐ๊ธˆ ๋” ์‚ดํŽด๋ณด๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ์—๋Š” main() ๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋“ค์ด ์ „๋‹ฌ๋œ๋‹ค๊ณ  ํ•˜๋ฉฐ ์ด๋•Œ ์ธ์ž๋Š” ๋ชจ๋‘ String ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
 
๋ณดํ†ต ๊ฐœ๋ฐœ์ž๋“ค์€ key=value ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜๋Š”๋ฐ CommandLineRunner๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด key=value ํ˜•ํƒœ๋กœ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์—ฌ๋„ 
"key=value" ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ์ธ์ž๊ฐ€ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ง์ ‘ key=value๋กœ ํŒŒ์‹ฑ ํ•˜๋Š” ์ž‘์—…์„ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
 
๊ทธ๋ž˜์„œ ์œ„์— ์„ค๋ช…์„ ๋ณด๋ฉด ๋งŒ์•ฝ raw String [] ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ApplicationArguments ํ˜•์‹์œผ๋กœ ์ ‘๊ทผํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ApplicationRunner๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๊ถŒ์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
 

ApplicationRunner

ApplicationRunner๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด CommandLineRunner๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์˜ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ApplicationRunner๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด CommandLineRunner์™€ ๋™์ผํ•˜๊ฒŒ run() ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•ํƒœ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
String [] ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ApplicationArguments ๊ฐ์ฒด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
 
ApplicationArguments๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด interface๋กœ ๋˜์–ด ์žˆ์œผ๋ฉฐ DefaultApplicationArguments๋ผ๊ณ  ํ•œ ๊ฐœ์˜ ๊ตฌํ˜„์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์•ž์„œ CommandLineRunner๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ „๋‹ฌ๋œ ์ธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  key=value ํ˜•ํƒœ๋กœ ํŒŒ์‹ฑ ํ•˜๋Š” ์ž‘์—…์„ ๊ฐœ๋ฐœ์ž๋“ค์ด ์ง์ ‘ ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ,
DefaultApplicationArguments ๊ฐ์ฒด๊ฐ€ ๋ฐ”๋กœ ์ด ์ž‘์—…์„ ๋Œ€์‹  ์ˆ˜ํ–‰ํ•ด ์ค๋‹ˆ๋‹ค.
๋Œ€์‹  ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฅด๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋•Œ ๋“ฑ์žฅํ•˜๋Š” ๊ฐœ๋…์ด CommandLine Arguments์™€ CommandLine Option Arguments์ž…๋‹ˆ๋‹ค.
 
CommandLineRunner์—์„œ ์ „๋‹ฌํ–ˆ๋˜ raw String ํ˜•ํƒœ์˜ ์ธ์ž๋“ค์„ CommandLine Arguments๋ผ๊ณ  ํ•˜๋ฉฐ ApplicationArguments์— ์˜ํ•ด key=value๋กœ ํŒŒ์‹ฑ ๋˜๋Š” ์ธ์ž๋“ค์„ CommandLine Option Arguments๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
 
CommandLine Option Arguments๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ธ์ž์— --์™€ ๊ฐ™์ด ํ•˜์ดํ”ˆ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ key=value ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
SimpleCommandLinePropertySource ๊ฐ์ฒด๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด ์ด์— ๊ด€ํ•ด ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…์ด ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

CommandLine Option Arguments๋Š” ๋ฐ˜๋“œ์‹œ --optName[=optValue] ํ˜•ํƒœ๋กœ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•˜๋ฉฐ =๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์–‘ ์˜†์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด ์žˆ์œผ๋ฉด ์•ˆ ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
 
์ •๋ฆฌํ•˜๋ฉด --์—†์ด ์ „๋‹ฌ๋˜๋Š” ์ธ์ž๋“ค์€ CommandLine Arguments, --๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „๋‹ฌ๋˜๋Š” ์ธ์ž๋“ค์€ CommandLine Option Arguments๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

testA: CommandLine Arguments
testA=testB: CommandLine Arguments

--testA: CommandLine Option Arguments
-- testA=testB: Command Line Option Arguments

๊ทธ๋ž˜์„œ ApplicationArguments์˜ ๊ตฌํ˜„์ฒด์ธ DefaultApplicationArguments ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธ์ž๋“ค์„ key=value ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ํŒŒ์‹ฑ ํ•˜์ง€ ์•Š์€ raw String [] ํ˜•ํƒœ์˜ CommandLine Arguments๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

DefaultApplicationArguments

getSourceArgs()
CommandLine Arguments๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

getOptionNames()
CommandLine Option Arguments์˜ name๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
--foo=bar --debug์˜ ๊ฒฝ์šฐ ["foo", "debug"]๊ฐ€ Set ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

getOptionValues(String name)
name์— ํ•ด๋‹นํ•˜๋Š” Arguments์˜ ๊ฐ’๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
--foo=bar์˜ ๊ฒฝ์šฐ ["bar"]๊ฐ€, --foo=bar,baz์˜ ๊ฒฝ์šฐ ["bar", "baz"]๊ฐ€ List ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

getNonOptionArgs()
CommandLine Option Arguments๊ฐ€ ์•„๋‹Œ ์ธ์ž๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
--foo=bar testA testB=B์˜ ๊ฒฝ์šฐ ["testA", "testB=B"]๊ฐ€ List ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

 

@EventListener

๋งˆ์ง€๋ง‰ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” @EventListener annotation์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. @EventListener๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด CommandLineRunner, ApplicationRunner๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด ์–ด๋–ค Event์— ์˜ํ•ด ์‹คํ–‰๋ ์ง€ ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ํ•  method๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

@EventListener(ApplicationReadyEvent.class)
public void init() {
    System.out.println("Hello ApplicationReadyEvent");
}

@EventListener annotation์—๋Š” ApplicationEvent ํƒ€์ž…์„ ์ž‘์„ฑํ•˜๋ฉด ๋˜๋Š”๋ฐ Spring Boot์—์„œ ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•ด ๋†“์€ Event ๊ฐ์ฒด๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

@EventListener
public void init(ApplicationReadyEvent event) {
    System.out.println("Hello ApplicationReadyEvent : " + event);
}

๊ทธ๋ฆฌ๊ณ  ์œ„์™€ ๊ฐ™์ด Event ๊ฐ์ฒด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ๊ฒฝ์šฐ์—๋Š” @EventListener annotation์— Event ํด๋ž˜์Šค๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋˜ ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ Event๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ํ•ด๋‹น method๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ํ•˜๋‚˜์˜ Event์— ์˜ํ•ด ์—ฌ๋Ÿฌ method๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


// Multi Event Listener - 1. ์—ฌ๋Ÿฌ Event๋ฅผ ํ•˜๋‚˜์˜ Method์—์„œ ์ˆ˜์‹ ํ•˜๊ธฐ
@EventListener({ ApplicationReadyEvent.class, MyEvent.class })
public void init() {
    System.out.println("Hello ApplicationReadyEvent || Hello My Event");
}

// Multi Event Listener - 2. ํ•˜๋‚˜์˜ Event๋ฅผ ์—ฌ๋Ÿฌ Method์—์„œ ์ˆ˜์‹ ํ•˜๊ธฐ
// EventListener ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Broadcasting ์ด๋‹ค.
@EventListener(MyEvent.class)
public void onMyEvent(MyEvent event) {
    System.out.println("Hello My Event: " + event.getMessage());
}

@EventListener(MyEvent.class)
public void onMyEvent2(MyEvent event) {
    System.out.println("Hello My Event2: " + event.getMessage());
}

 

'Spring > Spring Boot' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

@EnableWebMvc ๋Š” ์กฐ์‹ฌํ•ด์„œ ์‚ฌ์šฉํ•˜์ž  (0) 2023.05.31
@ControllerAdvice & @RestControllerAdvice  (0) 2023.04.23
Actuator (์•ก์ธ„์—์ดํ„ฐ)  (0) 2023.04.02

๋Œ“๊ธ€