๊ด€๋ฆฌ ๋ฉ”๋‰ด

์‚ถ์˜ ๊ณต์œ 

[Spring] DI์—์„œ ์™ธ๋ถ€ ์„ค์ • ์ฃผ์ž…ํ•˜๊ธฐ: @ConfigurationProperties ํ™œ์šฉ ๋ณธ๋ฌธ

Web Dev/BackEnd

[Spring] DI์—์„œ ์™ธ๋ถ€ ์„ค์ • ์ฃผ์ž…ํ•˜๊ธฐ: @ConfigurationProperties ํ™œ์šฉ

dkrehd 2025. 4. 29. 19:42
728x90
๋ฐ˜์‘ํ˜•

๐ŸŒฑ Spring DI์—์„œ ์™ธ๋ถ€ ์„ค์ • ์ฃผ์ž…ํ•˜๊ธฐ: @ConfigurationProperties ํ™œ์šฉ ๊ฐ€์ด๋“œ

์•ˆ๋…•ํ•˜์„ธ์š”! Spring (ํŠนํžˆ Spring Boot) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋ฉด, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์† ์ •๋ณด, API ํ‚ค, ์„œ๋ฒ„ ๋„๋ฉ”์ธ ๋“ฑ ๋‹ค์–‘ํ•œ ์„ค์ • ๊ฐ’๋“ค์„ ์ฝ”๋“œ์™€ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ฆฌํ•ด์•ผ ํ•  ํ•„์š”์„ฑ์„ ๋А๋ผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ, ํ…Œ์ŠคํŠธ, ์šด์˜ ํ™˜๊ฒฝ๋งˆ๋‹ค ๋‹ค๋ฅธ ์„ค์ •์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ฑฐ๋‚˜, ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ์ฝ”๋“œ์— ์ง์ ‘ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ์ฃ .

Spring์€ ์ด๋Ÿฌํ•œ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด application.properties ๋˜๋Š” application.yml ๊ฐ™์€ ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ์„ ํ™œ์šฉํ•˜๋Š” ๊ฐ•๋ ฅํ•˜๊ณ  ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ๊ทธ์ค‘์—์„œ๋„ ํƒ€์ž…-์„ธ์ดํ”„(Type-Safe) ํ•˜๊ฒŒ ์™ธ๋ถ€ ์„ค์ • ๊ฐ’์„ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” @ConfigurationProperties ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ์˜ˆ์ œ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์™œ ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ์„ ์‚ฌ์šฉํ• ๊นŒ์š”?

์ฝ”๋“œ ์•ˆ์— ์„ค์ • ๊ฐ’์„ ์ง์ ‘ ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋“ค์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ํ™˜๊ฒฝ๋ณ„ ์„ค์ • ๋ณ€๊ฒฝ์˜ ์–ด๋ ค์›€: ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์šด์˜ ํ™˜๊ฒฝ์˜ DB ์ •๋ณด๋‚˜ ์„œ๋ฒ„ ์ฃผ์†Œ๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ, ๋งค๋ฒˆ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ๋‹ค์‹œ ๋นŒ๋“œ/๋ฐฐํฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณด์•ˆ ๋ฌธ์ œ: API ํ‚ค๋‚˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ™์€ ๋ฏผ๊ฐํ•œ ์ •๋ณด๊ฐ€ ์ฝ”๋“œ์— ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์œ ์ง€๋ณด์ˆ˜ ์–ด๋ ค์›€: ์„ค์ • ๊ฐ’์ด ์ฝ”๋“œ ๊ณณ๊ณณ์— ํฉ์–ด์ ธ ์žˆ์œผ๋ฉด ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ต๊ณ  ๋ณ€๊ฒฝ ์‹œ ๋ˆ„๋ฝ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ณ , ์„ค์ •์„ ์ค‘์•™์—์„œ ๊ด€๋ฆฌํ•˜๋ฉฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์œ ์—ฐ์„ฑ๊ณผ ๋ณด์•ˆ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Spring์˜ ํ•ด๊ฒฐ์ฑ…: @ConfigurationProperties

Spring Boot๋Š” application.properties ๋‚˜ application.yml ํŒŒ์ผ์„ ํด๋ž˜์ŠคํŒจ์Šค ๋ฃจํŠธ(src/main/resources)์— ๋‘๋ฉด ์ž๋™์œผ๋กœ ๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ @Value ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ณ„ ์†์„ฑ์„ ํ•˜๋‚˜์”ฉ ์ฃผ์ž…๋ฐ›๋Š” ๋ฐฉ์‹์€ ์†์„ฑ์ด ๋งŽ์•„์งˆ์ˆ˜๋ก ๋ฒˆ๊ฑฐ๋กญ๊ณ  ํƒ€์ž… ์•ˆ์ •์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@ConfigurationProperties๋Š” ๊ด€๋ จ ์žˆ๋Š” ์„ค์ • ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ์ž๋ฐ” ๊ฐ์ฒด(POJO) ๋กœ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํ›จ์”ฌ ์„ธ๋ จ๋œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ๋‹จ๊ณ„:

  1. ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ ์ •์˜ (application.properties)
  2. ์„ค์ • ๊ฐ’์„ ๋‹ด์„ ์ž๋ฐ” ํด๋ž˜์Šค ์ƒ์„ฑ ๋ฐ @ConfigurationProperties ์ ์šฉ (MyProperties.java)
  3. ํ•ด๋‹น Properties ํด๋ž˜์Šค๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  ๋ฐ”์ธ๋”ฉ ํ™œ์„ฑํ™” (@EnableConfigurationProperties in Main.java)
  4. ์„ค์ • ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉ (@Autowired in Main.java)

์ด์ œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ ๋‹จ๊ณ„๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ  ์˜ˆ์‹œ ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์„ค์ • ์ฃผ์ž… ๋ฐฉ๋ฒ•

๐Ÿ”ธ (1) application.properties ํŒŒ์ผ ์ž‘์„ฑ

๋จผ์ €, src/main/resources ํด๋” ์•„๋ž˜์— application.properties ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์„ค์ • ๊ฐ’๋“ค์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ด€๋ จ ์žˆ๋Š” ์†์„ฑ๋“ค์„ ๊ณตํ†ต๋œ ์ ‘๋‘์‚ฌ(prefix) ๋กœ ์‹œ์ž‘ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” mysite๋ฅผ ์ ‘๋‘์‚ฌ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 
# src/main/resources/application.properties

mysite.domain=www.ABC.com
mysite.email=admin@ABC.com

 

๐Ÿ”ธ (2) ์„ค์ • ์ •๋ณด๋ฅผ ๋‹ด์„ ํด๋ž˜์Šค ์ž‘์„ฑ  (MyProperties.java)

 

๋‹ค์Œ์œผ๋กœ, application.properties ํŒŒ์ผ์˜ mysite ์ ‘๋‘์‚ฌ๋ฅผ ๊ฐ€์ง„ ์†์„ฑ๋“ค์„ ๋‹ด์„ ์ž๋ฐ” ํด๋ž˜์Šค(MyProperties)๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

package com.yourpackage; // ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋Š” ์‹ค์ œ ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•˜์„ธ์š”.

import org.springframework.boot.context.properties.ConfigurationProperties;

// @ConfigurationProperties(prefix="mysite"): "mysite" ์ ‘๋‘์‚ฌ๋ฅผ ๊ฐ€์ง„ ์†์„ฑ๋“ค์„ ์ด ํด๋ž˜์Šค์˜ ํ•„๋“œ์— ๋ฐ”์ธ๋”ฉํ•ฉ๋‹ˆ๋‹ค.
@ConfigurationProperties(prefix = "mysite")
public class MyProperties {
    private String domain; // mysite.domain ๊ฐ’๊ณผ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค.
    private String email;  // mysite.email ๊ฐ’๊ณผ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค.

    // Spring์ด ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ ์œ„ํ•ด Getter์™€ Setter๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // (toString() ๋ฉ”์„œ๋“œ ๋“ฑ์€ ํ•„์š”์— ๋”ฐ๋ผ ์ถ”๊ฐ€)
}
  • @ConfigurationProperties(prefix = "mysite"): ์ด ์–ด๋…ธํ…Œ์ด์…˜์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค! Spring Boot์—๊ฒŒ "application.properties (๋˜๋Š” ๋‹ค๋ฅธ ์™ธ๋ถ€ ์„ค์ • ์†Œ์Šค)์—์„œ mysite. ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์†์„ฑ๋“ค์„ ์ฐพ์•„์„œ ์ด ํด๋ž˜์Šค์˜ ํ•„๋“œ์— ์ž๋™์œผ๋กœ ์ฑ„์›Œ ๋„ฃ์–ด์ค˜!" ๋ผ๊ณ  ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.
  • ํ•„๋“œ ์ด๋ฆ„ ๋งคํ•‘: Spring Boot๋Š” ์œ ์—ฐํ•œ ๋ฐ”์ธ๋”ฉ ๊ทœ์น™์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ mysite.some-property ๋˜๋Š” mysite.someProperty๋Š” someProperty ํ•„๋“œ์™€ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค. (์˜ˆ: mysite.domain -> domain ํ•„๋“œ)
  • Getter/Setter ํ•„์ˆ˜: Spring์ด ๋ฆฌํ”Œ๋ ‰์…˜์„ ํ†ตํ•ด ๊ฐ’์„ ์ฃผ์ž…ํ•˜๋ ค๋ฉด ํ•ด๋‹น ํ•„๋“œ์— ๋Œ€ํ•œ public Getter์™€ Setter๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (Lombok์˜ @Getter, @Setter ๋˜๋Š” @Data๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.)

๐Ÿ”ธ (3) ๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž‘์„ฑ

@ConfigurationProperties ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ ํด๋ž˜์Šค๊ฐ€ ์‹ค์ œ๋กœ ๋™์ž‘(์†์„ฑ ๊ฐ’ ๋ฐ”์ธ๋”ฉ)ํ•˜๊ณ  ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜๊ฒŒ ํ•˜๋ ค๋ฉด, ์ด ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. @EnableConfigurationProperties ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต @Configuration ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ์„ค์ • ํด๋ž˜์Šค๋‚˜ ๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 
package com.yourpackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

// @EnableConfigurationProperties({MyProperties.class}): MyProperties ํด๋ž˜์Šค์— ๋Œ€ํ•œ @ConfigurationProperties ์ฒ˜๋ฆฌ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ณ ,
// MyProperties๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.
@EnableConfigurationProperties({MyProperties.class})
@Configuration // ์ด ํด๋ž˜์Šค๊ฐ€ ์„ค์ • ์ •๋ณด๋ฅผ ํฌํ•จํ•จ์„ ์•Œ๋ฆผ
@ComponentScan // @Component ๋“ฑ์„ ์Šค์บ”ํ•˜์—ฌ ๋นˆ์œผ๋กœ ๋“ฑ๋ก (์˜ˆ์ œ์—์„œ๋Š” MyProperties ์™ธ ๋‹ค๋ฅธ ๋นˆ์€ ์—†์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ)
public class Main implements CommandLineRunner { // CommandLineRunner: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ํ›„ ํŠน์ • ์ฝ”๋“œ ์‹คํ–‰

    @Autowired
    MyProperties prop; // 4๋‹จ๊ณ„: ์„ค์ • ๊ฐ์ฒด ์ฃผ์ž…

    @Autowired
    ApplicationContext ac; // ApplicationContext ์ฃผ์ž… (๋นˆ ๋ชฉ๋ก ํ™•์ธ์šฉ)

    // CommandLineRunner์˜ run ๋ฉ”์„œ๋“œ: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๊ณ  DI๊ฐ€ ์™„๋ฃŒ๋œ ํ›„ ์‹คํ–‰๋จ
    @Override
    public void run(String... args) throws Exception {
        // ๋“ฑ๋ก๋œ ๋นˆ ๋ชฉ๋ก ํ™•์ธ (๋””๋ฒ„๊น… ๋ชฉ์ )
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        Arrays.sort(beanDefinitionNames);
        Arrays.stream(beanDefinitionNames)
                .filter(b -> !b.startsWith("org")) // Spring ๋‚ด๋ถ€ ๋นˆ ์ œ์™ธ
                .forEach(System.out::println); // ๋นˆ ์ด๋ฆ„ ์ถœ๋ ฅ

        // ์ฃผ์ž…๋ฐ›์€ MyProperties ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์„ค์ • ๊ฐ’ ์‚ฌ์šฉ
        System.out.println("prop.getDomain() = " + prop.getDomain());
        System.out.println("prop.getEmail() = " + prop.getEmail());
    }

    public static void main(String[] args) {
        // Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
        ApplicationContext context = SpringApplication.run(Main.class, args);
    }
}
  • @EnableConfigurationProperties({MyProperties.class}): ์ด ์–ด๋…ธํ…Œ์ด์…˜์ด ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. Spring์—๊ฒŒ MyProperties ํด๋ž˜์Šค์— ์ •์˜๋œ @ConfigurationProperties ์„ค์ •์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์ง€์‹œํ•˜๊ณ , ๋™์‹œ์— MyProperties ์ž์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ @Autowired๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ”ธ (4) ์„ค์ • ๊ฐ์ฒด ์ฃผ์ž… ๋ฐ ์‚ฌ์šฉ 

์ด์ œ @EnableConfigurationProperties ๋•๋ถ„์— ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜๊ณ  ์™ธ๋ถ€ ์„ค์ • ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ง„ MyProperties ๊ฐ์ฒด๋ฅผ ํ•„์š”ํ•œ ๊ณณ์—์„œ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Java
 
    // ... Main ํด๋ž˜์Šค ๋‚ด๋ถ€ ...

    @Autowired // ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” MyProperties ๋นˆ์„ ์ž๋™์œผ๋กœ ์ฃผ์ž…๋ฐ›์Œ
    MyProperties prop;

    @Override
    public void run(String... args) throws Exception {
        // ์ฃผ์ž…๋ฐ›์€ prop ๊ฐ์ฒด์˜ getter ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์„ค์ • ๊ฐ’์— ์ ‘๊ทผ
        System.out.println("prop.getDomain() = " + prop.getDomain()); // ์ถœ๋ ฅ: prop.getDomain() = www.fastcampus.com
        System.out.println("prop.getEmail() = " + prop.getEmail());   // ์ถœ๋ ฅ: prop.getEmail() = admin@fascampus.com
    }
  • @Autowired MyProperties prop;: Spring์ด ๊ด€๋ฆฌํ•˜๋Š” MyProperties ๋นˆ์„ ์ฐพ์•„ prop ํ•„๋“œ์— ์ฃผ์ž…ํ•ด์ค๋‹ˆ๋‹ค. ์ด ์‹œ์ ์˜ prop ๊ฐ์ฒด๋Š” ์ด๋ฏธ application.properties ํŒŒ์ผ์˜ ๊ฐ’๋“ค๋กœ ์ฑ„์›Œ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • CommandLineRunner ์‚ฌ์šฉ ์ด์œ : main ๋ฉ”์„œ๋“œ๋Š” static ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ @Autowired ํ•„๋“œ(prop)๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. CommandLineRunner ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  run ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๋ฉด, Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๊ณ  ๋ชจ๋“  ๋นˆ์˜ ์ƒ์„ฑ๊ณผ ์˜์กด์„ฑ ์ฃผ์ž…์ด ์™„๋ฃŒ๋œ ํ›„์— run ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ run ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ๋Š” ์•ˆ์ „ํ•˜๊ฒŒ @Autowired๋œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(prop)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@ConfigurationProperties์˜ ์žฅ์ 

  • ํƒ€์ž… ์•ˆ์ „์„ฑ (Type Safety): ๋ฌธ์ž์—ด ๊ธฐ๋ฐ˜์˜ @Value์™€ ๋‹ฌ๋ฆฌ, ์ •์˜๋œ ํด๋ž˜์Šค์˜ ํƒ€์ž…(String, int, List ๋“ฑ)์œผ๋กœ ๊ฐ’์„ ๋ฐ›์•„์˜ค๋ฏ€๋กœ ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ์ปดํŒŒ์ผ ์‹œ์  ๋˜๋Š” IDE์—์„œ ๋ฏธ๋ฆฌ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ ๊ฐ€๋…์„ฑ ๋ฐ ๊ตฌ์กฐํ™”: ๊ด€๋ จ๋œ ์„ค์ • ๊ฐ’๋“ค์ด ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋กœ ๋ฌถ์—ฌ์žˆ์–ด ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ดํ•˜๊ณ  ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์ง‘๋‹ˆ๋‹ค.
  • IDE ์ง€์›: @ConfigurationProperties ํด๋ž˜์Šค๋Š” IDE์˜ ์ž๋™ ์™„์„ฑ ๋ฐ ๊ฒ€์ฆ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค. (application.properties์—์„œ ํ‚ค ์ž๋™์™„์„ฑ ๋“ฑ)
  • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ (Validation): JSR-303(Bean Validation) ์–ด๋…ธํ…Œ์ด์…˜(@NotNull, @Email, @Max ๋“ฑ)์„ Properties ํด๋ž˜์Šค ํ•„๋“œ์— ์ถ”๊ฐ€ํ•˜์—ฌ ์„ค์ • ๊ฐ’์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์˜ˆ์ œ์—๋Š” ์—†์ง€๋งŒ ๋งค์šฐ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ)
  • ์œ ์—ฐํ•œ ๋ฐ”์ธ๋”ฉ: some-property, someProperty, SOME_PROPERTY ๋“ฑ ๋‹ค์–‘ํ•œ ํ˜•์‹์˜ ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๋™์ผํ•œ ํ•„๋“œ(someProperty)์— ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

Spring (ํŠนํžˆ Spring Boot)์—์„œ ์™ธ๋ถ€ ์„ค์ • ๊ฐ’์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ€์žฅ ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” @ConfigurationProperties๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์„ค์ • ๊ฐ’๋“ค์„ ํƒ€์ž…-์„ธ์ดํ”„ํ•œ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ์‰ฝ๊ฒŒ ๋ฐ”์ธ๋”ฉํ•˜๊ณ , ์ฝ”๋“œ์™€ ์„ค์ •์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์œ ์—ฐ์„ฑ, ๊ฐ€๋…์„ฑ, ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•