23.7 Application 인자에 접근하기
SpringApplication.run(...) 을 이용해 전송된 어플리케이션 인수들에 접근하려면 org.springframework.boot.ApplicationArguments 빈을 주입 받으면 된다. ApplicationArguments 인터페이스가 인수의 원래 타입인 String[]의 인수나 그것을 정제한parsing option 과 non-option 타입의 인수 둘 다로 접근하는 방법을 제공한다. 다음은 그 예제코드다.
import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
}
}![[팁]](https://wannaqueen.gitbook.io/spring5/~gitbook/image?url=https%3A%2F%2Fdocs.spring.io%2Fspring-boot%2Fdocs%2Fcurrent%2Freference%2Fhtmlsingle%2Fimages%2Ftip.png&width=300&dpr=4&quality=100&sign=e19e9545&sv=2)
스프링 부트는 CommandLinePropertySource를 스프링 Environment에 등록할 수있다. 그렇게 하면 @Value 어노테이션을 사용해 하나의 어플리케이션 인수들을 주입할 수있다.
Last updated
Was this helpful?