Home > Spring > ๐Ÿƒ[Spring] `@Configuration`์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿƒ[Spring] `@Configuration`์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?
Spring Framework

๐Ÿƒ[Spring] @Configuration์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

  • @Configuration์€ Spring Framework์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์• ๋„ˆํ…Œ์ด์…˜์œผ๋กœ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์„ค์ • ํด๋ž˜์Šค์ž„์„ ๋ช…์‹œํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด ์• ๋„ˆํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋Š” Bean ์„ค์ • ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ๊ฐœ๋ฐœ์ž๋Š” application.properties๋‚˜ application.yml ์„ค์ •์„ ๋Œ€์‹ ํ•˜์—ฌ Java ๊ธฐ๋ฐ˜์˜ ์„ค์ •์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์žฌ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    • ๊ทธ๋Ÿฌ๋‚˜ @Configuration์ด application.properties๋‚˜ application.yml ํŒŒ์ผ ์ž์ฒด๋ฅผ ์™„์ „ํžˆ ๋Œ€์ฒดํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.
      • ์ด ๋‘ ๊ฐ€์ง€๋Š” ์„œ๋กœ ๋ณด์™„์ ์ธ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

1๏ธโƒฃ @Configuration๊ณผ application.properties/application.yml์˜ ๊ด€๊ณ„

1๏ธโƒฃ application.properties/application.yml

  • ์ฃผ๋กœ ํ™˜๊ฒฝ ์„ค์ • ๊ฐ’์„ ์™ธ๋ถ€ํ™”ํ•˜์—ฌ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค URL, ์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ, ํฌํŠธ์™€ ๊ฐ™์€ ์„ค์ •๊ฐ’์„ ๋‹ด์Šต๋‹ˆ๋‹ค.
  • ํŒŒ์ผ ๊ธฐ๋ฐ˜ ์„ค์ •์ด๋ฉฐ, ์Šคํ”„๋ง์ด ์ž๋™์œผ๋กœ ์ฝ์–ด์„œ ํ•„์š”ํ•œ ๊ณณ์— ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ @Configuration

  • Java ์ฝ”๋“œ๋กœ ๋ช…์‹œ์ ์œผ๋กœ Bean๊ณผ ์„ค์ •์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • @Bean ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋™์ ์ธ ์„ค์ • ๋กœ์ง์ด๋‚˜ ๋ณต์žกํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋กœ์ง์„ ๋‹ด์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์„ค์ •๊ฐ’์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ตฌ์„ฑํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์˜ˆ์ œ: Java ๊ธฐ๋ฐ˜ ์„ค์ •์œผ๋กœ application.properties ๋Œ€์ฒดํ•˜๊ธฐ.

๐Ÿ‘‰ ๊ธฐ์กด application.properties ์„ค์ •.

app.name=MyApplication
app.version=1.0.0

๐Ÿ‘‰ Java ๊ธฐ๋ฐ˜ ์„ค์ •์œผ๋กœ ๋Œ€์ฒด.

@Configuration
public class AppConfig {
    
    @Bean
    public String appName() {
        return "MyApplication";
    }
    
    @Bean
    public String appVersion() {
        return "1.0.0";
    }
}
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ appName๊ณผ appVersion์ด๋ผ๋Š” ์ด๋ฆ„์˜ Bean์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ @Configuration๊ณผ application.properties๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.

  • ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, Java ๊ธฐ๋ฐ˜ ์„ค์ •์€ application.properties๋‚˜ application.yml์— ์ •์˜๋œ ๊ฐ’์„ ์ฝ์–ด์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

1๏ธโƒฃ ์˜ˆ์ œ: @Configuration์—์„œ application.properties ์‚ฌ์šฉ

1๏ธโƒฃ application.properties ํŒŒ์ผ.

app.name=MyApplication
app.version=1.0.0

2๏ธโƒฃ @Configuration ํด๋ž˜์Šค

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    
    @Value("${app.name}")
    private String appName;
    
    @Value("${app.version}")
    private String appVersion;
    
    @Bean
    public AppInfo appInfo() {
        return new AppInfo(appName, appVersion);
    }
    
    public static class AppInfo {
        private final String name;
        private final String version;
        
        public AppInfo(String name, String version) {
            this.name = name;
            this.version = version;
        }
        
        public String getName() {
            return name;
        }
        
        public String getVersion() {
            return version;
        }
    }
}
  • ์ด ๋ฐฉ์‹์—์„œ๋Š” application.properties์— ์ •์˜๋œ ๊ฐ’์„ @Value๋กœ ์ฃผ์ž…๋ฐ›์•„ Java ์ฝ”๋“œ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4๏ธโƒฃ ์–ธ์ œ Java ๊ธฐ๋ฐ˜ ์„ค์ •์„ ์‚ฌ์šฉํ• ๊นŒ?

  • ๋™์  ๋กœ์ง์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ.
    • ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ Bean์„ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ.
    • ๋ณต์žกํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋กœ์ง์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ.
  • ์ฝ”๋“œ๋กœ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •์„ ๊ด€๋ฆฌํ•˜๋ ค๋Š” ๊ฒฝ์šฐ.
    • ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ์ด ์•„๋‹Œ, ์ฝ”๋“œ์—์„œ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ์ ํ•ฉํ•œ ๊ฒฝ์šฐ.

5๏ธโƒฃ ์–ธ์ œ application.properties๋ฅผ ์‚ฌ์šฉํ• ๊นŒ?

  • ํ™˜๊ฒฝ๋ณ„ ์„ค์ •์„ ๊ด€๋ฆฌํ•  ๋•Œ
    • ๊ฐœ๋ฐœ/์šด์˜ ํ™˜๊ฒฝ์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•˜๋ ค๋Š” ๊ฒฝ์šฐ.
  • ๊ฐ„๋‹จํ•œ ์„ค์ •๊ฐ’
    • ๋ฌธ์ž์—ด, ์ˆซ์ž, ๋…ผ๋ฆฌ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ์„ค์ •๊ฐ’.

6๏ธโƒฃ ๊ฒฐ๋ก .

  • @Configuration์„ ์‚ฌ์šฉํ•˜๋ฉด Java ์ฝ”๋“œ๋กœ ์„ค์ •์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ , ํŠน์ • ๊ฒฝ์šฐ์—๋Š” application.properties๋‚˜ application.yml์„ ๋Œ€์ฒดํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” Java ์„ค์ •๊ณผ application.properties/application.yml ํŒŒ์ผ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์œ ์—ฐ์„ฑ๊ณผ ๊ด€๋ฆฌ ํŽธ์˜์„ฑ์„ ๋ชจ๋‘ ์–ป๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค.