๐ก์ด ํฌ์คํ ์ ํ ๋น ๋์ ์ ํ๋ธ ์์์ธ ์คํ๋ง ๋ถํธ ์ฑ์ ์ด๊ธฐํ ์ฝ๋๋ฅผ ๋ฃ๋ ๋ฐฉ๋ฒ 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 |
๋๊ธ