1. ApplicationArguments
- Run/Debug Configurations -> Configuration -> VM options : -D로 들어옴
- Run/Debug Configurations -> Configuration -> Program arguments : --로 들어옴
@Component
public class ArgumentsTest {
public ArgumentsTest(ApplicationArguments arguments) {
System.out.println("foo: " + arguments.containsOption("foo"));
System.out.println("bar: " + arguments.containsOption("bar"));
}
}
- Bean의 생성자가 한 개이고, 생성자에 parameter가 Bean일 경우에는, 그 Bean을 Spring이 알아서 주입을 해 줌
- -D로 들어오는 건 JVM Option ex) -Ddebug
- JVM Option은 Application Arguments가 아님
- 실행하면, foo: false / bar: true 출력
2. ApplicationRunner
애플리케이션 실행 후에 뭔가를 실행하고 싶은 경우 사용
@Order 어노테이션으로 순서 지정 가능 ex) @Order(1)
@Component
public class ArgumentsTest implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("foo: " + args.containsOption("foo"));
System.out.println("bar: " + args.containsOption("bar"));
}
}
-> 실행하면 위와 동일하게 foo: false / bar: true 출력
3. CommandLineRunner
ApplicationRunner 사용하는 것을 추천
@Component
public class SampleListener implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
Arrays.stream(args).forEach(System.out::println);
}
}
-> 실행하면 --bar 출력
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard
스프링 부트 개념과 활용 - 인프런 | 강의
스프링 부트의 원리 및 여러 기능을 코딩을 통해 쉽게 이해하고 보다 적극적으로 사용할 수 있는 방법을 학습합니다., - 강의 소개 | 인프런...
www.inflearn.com
Core Features
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, an
docs.spring.io
Core Features
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, an
docs.spring.io
'Java > Spring Boot' 카테고리의 다른 글
[Spring Boot] 프로파일 (0) | 2022.03.13 |
---|---|
[Spring Boot] 외부 설정 (0) | 2022.03.13 |
[Spring Boot] WebApplicationType (0) | 2022.03.13 |
[Spring Boot] Log Level (0) | 2022.03.13 |
[Spring Boot] Application Events and Listener (0) | 2022.03.13 |