์Šคํ”„๋ง ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ• (์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜, ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜, ์Šคํ”„๋ง ํ†ตํ•ฉ)

2023. 5. 8. 09:34ใ†์ธํ”„๋Ÿฐ/์Šคํ”„๋ง ๋ถ€ํŠธ - ํ•ต์‹ฌ ์›๋ฆฌ์™€ ํ™œ์šฉ

728x90

 

์Šคํ”„๋ง ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ• (OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜, ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ) (tistory.com)

 

์Šคํ”„๋ง ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ• (OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜, ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ)

0๏ธโƒฃ ์™ธ๋ถ€ ์„ค์ •์ด๋ž€ ? ๋ณดํ†ต ์‹ค๋ฌด์—์„œ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด ๋Œ€๋ถ€๋ถ„ ํšŒ์‚ฌ์—์„œ ์šด์˜ ์„œ๋ฒ„์™€ ๊ฐœ๋ฐœ ์„œ๋ฒ„๋ฅผ ๋‚˜๋ˆ ์„œ ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•œ๋‹ค. ๋จผ์ € ๊ฐœ๋ฐœ ์„œ๋ฒ„์—์„œ ๊ฐœ๋ฐœํ•˜๊ณ , ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ ํ›„ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•œ

hyejin.tistory.com

์ €๋ฒˆ ๊ธ€์—์„œ๋Š” ์Šคํ”„๋ง ์™ธ๋ถ€ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜, ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์ฝ์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค๋‹ค. 

์ด๋ฒˆ์—๋Š” ๋‚˜๋จธ์ง€ ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ž‘์„ฑํ•  ์˜ˆ์ •์ด๋‹ค. 

 

 

1๏ธโƒฃ ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ• :  ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜ 

์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜ (Command line arguments)๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์‹œ์ ์— ์™ธ๋ถ€ ์„ค์ •๊ฐ’์„ main(args) ๋ฉ”์„œ๋“œ์˜ args ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

java -jar XXX.jar dataA dataB 

์ด๋Ÿฐ์‹์œผ๋กœ ๋’ค์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ŠคํŽ˜์ด์Šค๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ „๋‹ฌํ•˜๋ฉด args์— dataA, dataB ๋ฌธ์ž๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค. 

 

CommandLineV1

@Slf4j
public class CommandLineV1
{
   public static void main(String[] args)
   {
      for (String arg : args)
      {
         log.info("arg {}", arg);
      }
   }
}

์‹คํ–‰๊ฒฐ๊ณผ

์‹คํ–‰ ๊ฒฐ๊ณผ dataA, dataB๊ฐ€ ๋กœ๊ทธ๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๊ทผ๋ฐ ๋ณดํ†ต ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •์„ ํ•  ๋•Œ๋Š” key=vaue ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๋ฐ›๋Š” ๊ฒƒ์ด ๋” ํŽธ๋ฆฌํ•˜๊ณ  ์‹ค์ œ๋กœ ๊ทธ๋ ‡๊ฒŒ ๋” ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. 

๋”ฐ๋ผ์„œ dataA dataB ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•˜๋Š” ๊ฒƒ ๋ง๊ณ  url=devdb username=dev_user password=dev_pw ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๋‹ค์‹œ ์‹คํ–‰ํ•ด๋ณด๋ฉด 

key=vaue ํ˜•์‹์ด ์•„๋‹ˆ๋ผ ๊ทธ๋ƒฅ ๊ทธ๋Œ€๋กœ ์ ์€ ๋ฌธ์ž๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด ์ด๊ฑธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๋ถ„๋ฆฌํ•ด์„œ key=value ํ˜•์‹์œผ๋กœ ๋งž๊ฒŒ ๋ณ€ํ™˜ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋Ÿฌ์›€์ด ์žˆ๋‹ค. 

 

 

 

2๏ธโƒฃ ์™ธ๋ถ€์„ค์ • ๋ฐฉ๋ฒ• : ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜ 

์ผ๋ฐ˜์ ์ธ ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜๋Š” ๋„์–ด์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  key=value ํ˜•์‹์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์—†๋‹ค. 

๋”ฐ๋ผ์„œ ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜๋ฅผ key=value ํ˜•์‹์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ–ˆ๋Š”๋ฐ ์Šคํ”„๋ง์€ ์ด์ œ ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜๋ฅผ key=value ํ˜•์‹์œผ๋กœ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šคํ”„๋ง ๋งŒ์˜ ํ‘œ์ค€ ๋ฐฉ์‹์„ ์ •์˜ํ–ˆ๋‹ค.  -> ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜ 

 


์Šคํ”„๋ง์€ ์ปค๋งจ๋“œ ๋ผ์ธ์— -- ๋ฅผ ์—ฐ๊ฒฐํ•ด์„œ ์‹œ์ž‘ํ•˜๋ฉด key=value ํ˜•์‹์œผ๋กœ ์ •ํ•˜๊ณ  ์ด๊ฑธ ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. 

--url=devdb --username=dev_user --password=dev_pw 

(ํ•˜๋‚˜์˜ ํ‚ค์— ์—ฌ๋Ÿฌ๊ฐ’ ์„ค์ • ๊ฐ€๋Šฅ) 

 

CommandLineV2

@Slf4j
public class CommandLineV2
{
   // --url=devdb --username=dev_user --password=dev_pw mode=on
   public static void main(String[] args)
   {
      for (String arg : args)
      {
         log.info("arg {}", arg);
      }
      
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
      log.info("SourceArgs = {}", List.of(applicationArguments.getSourceArgs()));
      log.info("NonOptionArgs = {}", applicationArguments.getNonOptionArgs());
      log.info("OptionsNames = {} ", applicationArguments.getOptionNames());
      
      Set<String> optionNames = applicationArguments.getOptionNames();
      for (String optionName : optionNames)
      {
         log.info("option arg {}={}", optionName, applicationArguments.getOptionValues(optionName));
      }
      
      List<String> url = applicationArguments.getOptionValues("url");
      List<String> username = applicationArguments.getOptionValues("username");
      List<String> password = applicationArguments.getOptionValues("password");
      List<String> mode = applicationArguments.getOptionValues("mode");
      
      log.info("url = {}", url);
      log.info("username = {}", username);
      log.info("password = {}", password);
      log.info("mode = {}", mode);
   }
}

์Šคํ”„๋ง์ด ์ œ๊ณตํ•˜๋Š” ApplicationArguments ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ด๋ฅผ ๊ตฌํ˜„ํ•œ DefaultApplicationArguments ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

--urldb=devdb --username=dev_user --password=dev_pw mode=on ์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์ด๋•Œ mode ๋Š” ์•ž์— --๋ฅผ ๋ถ™์—ฌ์ฃผ์ง€ ์•Š์•˜๋‹ค. 

 

NonOptionArgs ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด --๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฆ‰, ์˜ต์…˜ ์ธ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒƒ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๊ณ , 

OptionNames๋ฅผ ํ•˜๋ฉด key ๊ฐ’์„, OptionValue(key) ๋ฅผ ํ•˜๋ฉด value ๊ฐ’์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

arg : ์ปค๋งจ๋“œ ๋ผ์ธ์˜ ์ž…๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค. 

SourceArgs : ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜ ์ „๋ถ€ ์ถœ๋ ฅ 

 

 

* ์˜ต์…˜ ์ธ์ˆ˜๋Š” --username=dev1 --username=dev2 ์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ํ‚ค์— ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋‹ด์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— applicationArguments.getOptionsValue(key) ์˜ ๊ฒฐ๊ณผ๊ฐ€ List๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

(๊ทธ๋ฆฌ๊ณ  ์ฐธ๊ณ ๋กœ ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๋Š” ์ž๋ฐ” ์–ธ์–ด์˜ ํ‘œ์ค€ ๊ธฐ๋Šฅ์ด ์•„๋‹ˆ๊ณ , ์Šคํ”„๋ง์ด ํŽธ๋ฆฌํ•จ์„ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค.) 

 

 

๐Ÿ’Œ ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ์ปค๋งจ๋“œ ๋ผ์ธ์„ ํฌํ•จํ•ด์„œ ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ApplicationArguments ๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด๋‘๊ณ , ํ•ด๋‹น ๋นˆ์„ ์ฃผ์ž…๋ฐ›์œผ๋ฉด ์ปค๋งจ๋“œ ๋ผ์ธ์œผ๋กœ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์–ด๋””์„œ๋“  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

CommandLineBean

@Slf4j
@Component
public class CommandLineBean
{
   private final ApplicationArguments arguments;
   
   public CommandLineBean(ApplicationArguments arguments)
   {
      this.arguments = arguments;
   }
   
   @PostConstruct
   public void init()
   {
      log.info("source {}", List.of(arguments.getSourceArgs()));
      log.info("optionNames {}", arguments.getOptionNames());
      Set<String> optionNames = arguments.getOptionNames();
      for (String optionName : optionNames)
      {
         log.info("option args {}={}", optionName, arguments.getOptionValues(optionName));
      }
   }
}

 

 

 

3๏ธโƒฃ ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ• : ์Šคํ”„๋ง ํ†ตํ•ฉ 

์ง€๊ธˆ๊นŒ์ง€ ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ•์œผ๋กœ OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜, ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ, ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜, ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜ ๋“ฑ์„ ๋ดค๋Š”๋ฐ ๋„“๊ฒŒ(?) ๋ณด๋ฉด ์™ธ๋ถ€ ์„ค์ • ๋ฐฉ๋ฒ•์€ key=value ํ˜•์‹์ด๊ณ  ์ด ์„ค์ •๊ฐ’์„ ์™ธ๋ถ€์— ์ง€์ •ํ•ด๋‘” ๊ฒƒ์ด๋‹ค. 

์œ„์—์„œ 4๊ฐ€์ง€ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ์™ธ๋ถ€ ์„ค์ •๊ฐ’ ํ˜•์‹์€ ๊ฐ™์€๋ฐ ์–ด๋””์— ์ง€์ •ํ•ด๋’€๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ๊ฐ๊ฐ ์ฝ๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. 

 

์˜ˆ๋ฅผ ๋“ค์–ด์„œ OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์„ค์ •๊ฐ’ ์ง€์ •ํ•ด๋’€๋‹ค๋ฉด System.getenv(key) ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ณ , ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ์œผ๋กœ ์™ธ๋ถ€๊ฐ’ ์„ค์ •ํ–ˆ๋‹ค๋ฉด 

System.getProperty(key) ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค...! ๋งŒ์•ฝ ์ง€๊ธˆ์€ ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ์— ์™ธ๋ถ€ ์„ค์ • ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. 

๊ทธ๋Ÿฐ๋ฐ ์ด์ œ ์ •์ฑ… ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•ด์„œ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ˆ˜๋กœ ์„ค์ • ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๋ฐ”๊ฟจ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด...

์ด์ œ ์ด ์ฝ”๋“œ๋“ค์„ ๋ชจ๋‘ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์กด์žฌํ•œ๋‹ค. 

 

-> ์™ธ๋ถ€ ์„ค์ •๊ฐ’์ด ์–ด๋””์— ์œ„์น˜ํ•˜๋“  ์ƒ๊ด€์—†์ด ์ผ๊ด€์„ฑ ์žˆ๊ณ , ํŽธ๋ฆฌํ•˜๊ฒŒ key=value ํ˜•์‹์˜ ์™ธ๋ถ€ ์„ค์ •๊ฐ’์„ ์ฝ์„ ์ˆ˜ ์žˆ์œผ๋ฉด ์ฝ”๋“œ ๋ณ€๊ฒฝ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋‹ˆ๊นŒ ๊ฐœ๋ฐœ์ž ์ž…์žฅ์—์„œ๋Š” ๋” ํŽธ๋ฆฌํ•˜๊ณ  ์™ธ๋ถ€ ์„ค์ •๊ฐ’ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋” ์œ ์—ฐํ•ด์ง„๋‹ค. 

์Šคํ”„๋ง์€ Environment์™€ PropertySource ๋ฅผ ํ†ตํ•ด ์ถ”์ƒํ™”๋ฅผ ํ•ด์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค! 

 

 

 

PropertySource 

์Šคํ”„๋ง์€ PropertySource ๋ผ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๊ฐ๊ฐ ์™ธ๋ถ€ ์„ค์ •์„ ์กฐํšŒํ•˜๋Š” ๊ตฌํ˜„์ฒด๋“ค์„ ๋งŒ๋“ค์—ˆ๋‹ค. 

CommandLinePropertySource

SystemEnviornmentPropertySource 

...

์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง์€ ๋กœ๋”ฉ ์‹œ์ ์— ํ•„์š”ํ•œ PropertySource๋“ค์„ ์ƒ์„ฑํ•˜๊ณ  Environment์—์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค. 

 

 

Environment

/**
 * Interface representing the environment in which the current application is running.
 * Models two key aspects of the application environment: <em>profiles</em> and
 * <em>properties</em>. Methods related to property access are exposed via the
 * {@link PropertyResolver} superinterface.
 *
 * <p>A <em>profile</em> is a named, logical group of bean definitions to be registered
 * with the container only if the given profile is <em>active</em>. Beans may be assigned
 * to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema
 * or the {@link org.springframework.context.annotation.Profile @Profile} annotation for
 * syntax details. The role of the {@code Environment} object with relation to profiles is
 * in determining which profiles (if any) are currently {@linkplain #getActiveProfiles
 * active}, and which profiles (if any) should be {@linkplain #getDefaultProfiles active
 * by default}.
 *
 * <p><em>Properties</em> play an important role in almost all applications, and may
 * originate from a variety of sources: properties files, JVM system properties, system
 * environment variables, JNDI, servlet context parameters, ad-hoc Properties objects,
 * Maps, and so on. The role of the {@code Environment} object with relation to properties
 * is to provide the user with a convenient service interface for configuring property
 * sources and resolving properties from them.
 *
 * <p>Beans managed within an {@code ApplicationContext} may register to be {@link
 * org.springframework.context.EnvironmentAware EnvironmentAware} or {@code @Inject} the
 * {@code Environment} in order to query profile state or resolve properties directly.
 *
 * <p>In most cases, however, application-level beans should not need to interact with the
 * {@code Environment} directly but instead may have to have {@code ${...}} property
 * values replaced by a property placeholder configurer such as
 * {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer
 * PropertySourcesPlaceholderConfigurer}, which itself is {@code EnvironmentAware} and
 * as of Spring 3.1 is registered by default when using
 * {@code <context:property-placeholder/>}.
 *
 * <p>Configuration of the {@code Environment} object must be done through the
 * {@code ConfigurableEnvironment} interface, returned from all
 * {@code AbstractApplicationContext} subclass {@code getEnvironment()} methods. See
 * {@link ConfigurableEnvironment} Javadoc for usage examples demonstrating manipulation
 * of property sources prior to application context {@code refresh()}.
 *
 * @author Chris Beams
 * @since 3.1
 * @see PropertyResolver
 * @see EnvironmentCapable
 * @see ConfigurableEnvironment
 * @see AbstractEnvironment
 * @see StandardEnvironment
 * @see org.springframework.context.EnvironmentAware
 * @see org.springframework.context.ConfigurableApplicationContext#getEnvironment
 * @see org.springframework.context.ConfigurableApplicationContext#setEnvironment
 * @see org.springframework.context.support.AbstractApplicationContext#createEnvironment
 */
public interface Environment extends PropertyResolver {

   /**
    * Return the set of profiles explicitly made active for this environment. Profiles
    * are used for creating logical groupings of bean definitions to be registered
    * conditionally, for example based on deployment environment. Profiles can be
    * activated by setting {@linkplain AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
    * "spring.profiles.active"} as a system property or by calling
    * {@link ConfigurableEnvironment#setActiveProfiles(String...)}.
    * <p>If no profiles have explicitly been specified as active, then any
    * {@linkplain #getDefaultProfiles() default profiles} will automatically be activated.
    * @see #getDefaultProfiles
    * @see ConfigurableEnvironment#setActiveProfiles
    * @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
    */
   String[] getActiveProfiles();

   /**
    * Return the set of profiles to be active by default when no active profiles have
    * been set explicitly.
    * @see #getActiveProfiles
    * @see ConfigurableEnvironment#setDefaultProfiles
    * @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME
    */
   String[] getDefaultProfiles();

   /**
    * Return whether one or more of the given profiles is active or, in the case of no
    * explicit active profiles, whether one or more of the given profiles is included in
    * the set of default profiles. If a profile begins with '!' the logic is inverted,
    * i.e. the method will return {@code true} if the given profile is <em>not</em> active.
    * For example, {@code env.acceptsProfiles("p1", "!p2")} will return {@code true} if
    * profile 'p1' is active or 'p2' is not active.
    * @throws IllegalArgumentException if called with zero arguments
    * or if any profile is {@code null}, empty, or whitespace only
    * @see #getActiveProfiles
    * @see #getDefaultProfiles
    * @see #acceptsProfiles(Profiles)
    * @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)}
    */
   @Deprecated
   boolean acceptsProfiles(String... profiles);

   /**
    * Return whether the {@linkplain #getActiveProfiles() active profiles}
    * match the given {@link Profiles} predicate.
    */
   boolean acceptsProfiles(Profiles profiles);

}

์Šคํ”„๋ง์€ Environment ๋ฅผ ์ œ๊ณตํ•ด์„œ ํŠน์ • ์™ธ๋ถ€ ์„ค์ •์— ์ข…์†๋˜์ง€ ์•Š๊ณ  ์ผ๊ด€์„ฑ์žˆ๊ฒŒ key=value ํ˜•์‹์˜ ์™ธ๋ถ€ ์„ค์ •์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

environment.getProperty(key)๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๊ณ , Environment๊ฐ€ ๋‚ด๋ถ€์—์„œ ์—ฌ๋Ÿฌ ๊ณผ์ •์„ ๊ฑฐ์ณ์„œ PropertySource์— ์ ‘๊ทผํ•œ๋‹ค. 

๋”ฐ๋ผ์„œ ์ด์ œ ๋ชจ๋“  ์™ธ๋ถ€ ์„ค์ •์€ Environment ๋ฅผ ํ†ตํ•ด์„œ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค. 

 

@Slf4j
@Component
public class EnvironmentCheck
{
   private final Environment environment;
   
   public EnvironmentCheck(Environment environment)
   {
      this.environment = environment;
   }
   
   @PostConstruct
   public void init()
   {
      String url = environment.getProperty("url");
      String username = environment.getProperty("username");
      String password = environment.getProperty("password");
      
      log.info("environment url = {}", url);
      log.info("environment username = {}", username);
      log.info("environment password = {}", password);
   }
   
}

์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ 

-Durl=devdb -Dusername=dev_user -Dpassword=dev_pw 

 

์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜ 

--url=devdb --username=dev_user --password=dev_pw 

 

๋กœ ๊ฐ๊ฐ ํ•œ๋ฒˆ์”ฉ ์ง€์ •ํ•ด์„œ ์‹คํ–‰ํ•ด๋ณด๋ฉด ๋‘˜๋‹ค ๋™์ผํ•˜๊ฒŒ Environment ๋ฅผ ํ†ตํ•ด์„œ ์„ค์ • ๊ฐ’์„ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋”ฐ๋ผ์„œ ์ด์ œ Environment๋กœ ์„ค์ • ๊ฐ’์„ ์ฝ์œผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€ ์„ค์ •๊ฐ’์ด ์ง€์ •ํ•˜๋Š” ๊ณณ์ด ๋ฐ”๋€Œ์–ด๋„ ์ฝ”๋“œ ๋ณ€๊ฒฝ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค! 

 

 

* ์šฐ์„ ์ˆœ์œ„ 

๋งŒ์•ฝ ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ๊ณผ ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜์— ๋™์‹œ์— ์„ค์ •๊ฐ’์„ ์ง€์ •ํ•ด๋’€๋‹ค๋ฉด 

์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๋กœ ์ง€์ •ํ•œ ์™ธ๋ถ€ ์„ค์ • ๊ฐ’์„ ์กฐํšŒํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

์šฐ์„ ์ˆœ์œ„๋Š” ๋ณดํ†ต ๋” ๋ฒ”์œ„๊ฐ€ ์ข์€ ๊ฒƒ์ด ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š”๋ฐ ์ž๋ฐ” ์‹œ์Šคํ…œ ์†์„ฑ์€ ํ•ด๋‹น JVM ๋‚ด์—์„œ ๋ชจ๋‘ ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๋Š” main์˜ arg๋ฅผ ํ†ตํ•ด์„œ ๋“ค์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ ‘๊ทผ ๋ฒ”์œ„๊ฐ€ ๋” ์ข๊ธฐ ๋•Œ๋ฌธ์— ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜ ์ธ์ˆ˜๊ฐ€ ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง„๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90