[Spring] AppConfig ๋ฆฌํŒฉํ„ฐ๋ง

2021. 9. 30. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘์ž…๋‹ˆ๋‹ค.

์ฃผ๋‹ˆํ•˜๋ž‘์€ ์•ž์—์„œ ์ฃผ๋ฌธ, ํšŒ์›, ํ• ์ธ(๊ณ ์ • ํ• ์ธ)์— ๋Œ€ํ•ด์„œ ์ˆœ์ˆ˜ ์ž๋ฐ”๋งŒ์„ ํ™œ์šฉํ•ด ์—ด์‹ฌํžˆ ๊ณต๋ถ€๋ฅผ ํ–ˆ๋˜ ๊ฒƒ์ด์—์š”.

์ด๋ฒˆ์—๋Š” AppCofing.java๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ๋ ค์ฃผ์…จ์–ด์š”!

ํ•จ๊ป˜ ๊ณต๋ถ€ ํ•ด ๋ณด์‹ค๊ฒŒ์š”!

 

 

๐Ÿ“‹ ๋ชฉ์ฐจ


01. [Spring] ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ
02. [Spring] ์ข‹์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์˜ 5๊ฐ€์ง€ ์›์น™ (SOLID)
03. 
[Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ์Šคํ”„๋ง   
04. [Spring] OCP์™€ DIP ๊ณ ๋ คํ•˜๊ธฐ
05. [Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์›๋ฆฌ ์ ์šฉ - ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ๊ฐœ๋ฐœ
06. 
[Spring] AppConfig ๋ฆฌํŒฉํ„ฐ๋ง    
07. [Spring] Ioc, DI Container  
08. [Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ  
09. [Spring] ์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ  
10. [Spring] Component Scan And Auto wired Start
11. 
[Spring] ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ๋ฒ•    
12. [Spring] Bean Life Cycle Call Back
13. 
[Spring] Bean Scope




 

 


 

๐Ÿ“Œ AppConfig ๋ฆฌํŒฉํ„ฐ๋ง


์šฐ๋ฆฌ๊ฐ€ ์•ž์—์„œ ์ž‘์„ฑํ•œ AppConfig๋Š” ํ˜„์žฌ ์ค‘๋ณต์ด ์žˆ๋Š” ์ƒํƒœ์ด๊ณ , ์—ญํ• ์— ๋”ฐ๋ฅธ ๊ตฌํ˜„์ด ์ž˜ ๋ณด์ด์ง€ ์•Š๋Š” ๊ฒƒ์ด์—์š”.

 

         ๐Ÿ‘‰ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ทธ๋ฆผ

๋ฐ˜์‘ํ˜•

 

 

         ๐Ÿ‘‰ ๋ฆฌํŒฉํ„ฐ๋ง ์ „

 
package hello.core;
import hello.core.discount.FixDiscountPolicy;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;

public class AppConfig {
 
       public MemberService memberService() {
          return new MemberServiceImpl(new MemoryMemberRepository());
	}

      public OrderService orderService() {
          return new OrderServiceImpl(
                  new MemoryMemberRepository(),
                  new FixDiscountPolicy());
	} 
}

 

 

         ๐Ÿ‘‰ ๋ฆฌํŒฉํ„ฐ๋ง ํ›„

 

package hello.core;
  import hello.core.discount.DiscountPolicy;
  import hello.core.discount.FixDiscountPolicy;
  import hello.core.member.MemberRepository;
  import hello.core.member.MemberService;
  import hello.core.member.MemberServiceImpl;
  import hello.core.member.MemoryMemberRepository;
  import hello.core.order.OrderService;
  import hello.core.order.OrderServiceImpl;
  
  public class AppConfig {
      public MemberService memberService() {
          return new MemberServiceImpl(memberRepository());
	}
      public OrderService orderService() {
          return new OrderServiceImpl(
                  memberRepository(),
                  discountPolicy());
  	}
      public MemberRepository memberRepository() {
          return new MemoryMemberRepository();
	}
      public DiscountPolicy discountPolicy() {
          return new FixDiscountPolicy();
	} 

}

์œ„์˜ ๋ฆฌํŒฉํ„ฐ๋ง ์ „์— ์ฝ”๋“œ์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ , ์—ญํ• ์— ๋”ฐ๋ฅธ ๊ตฌํ˜„์ด ์ž˜ ๋ณด์ผ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•œ ๊ฒƒ์ด์—์š”.

'new MemoryMemberRepository()' ์ด ๋ถ€๋ถ„์— ์ค‘๋ณต์ด ์ œ๊ฑฐ๋œ ๊ฒƒ์ด์—์š”. ์ด์ œ 'MemoryMemberRepository'๋ฅผ ๋‹ค๋ฅธ ๊ตฌํ˜„์ฒด๋กœ ๋ณ€๊ฒฝํ•  ๋•Œ ํ•œ ๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

'AppConfig'๋ฅผ ๋ณด๋ฉด ์—ญํ• ๊ณผ ๊ตฌํ˜„ Class๊ฐ€ ํ•œ ๋ˆˆ์— ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์ด์—์š”. Application ์ „์ฒด ๊ตฌ์„ฑ์ด ์–ด๋–ป๊ฒŒ ๋˜์–ด์žˆ๋Š”์ง€ ํ•œ ๋ˆˆ์— ํŒŒ์•…์ด ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค!

 

 

๐Ÿ“Œ ์ƒˆ๋กœ์šด ๊ตฌ์กฐ์™€ ํ• ์ธ ์ •์ฑ… ์ ์šฉ


์šฐ๋ฆฌ๋Š” ๋‹ค์‹œ ๊ณ ์ • ํ• ์ธ ์ •์ฑ…์„ ์ •๋ฅ  ํ• ์ธ ์ •์ฑ…์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š”๋ฐ, ์ง‘์ค‘ ํ•ด ๋ณด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

FixDicountPolicy.java์—์„œ RateDiscountPolicy.java๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

 

AppConfig์˜ ๋“ฑ์žฅ์œผ๋กœ Application์ด ํฌ๊ฒŒ ์‚ฌ์šฉ ์˜์—ญ๊ณผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ตฌ์„ฑ(Configuration)ํ•˜๋Š” ์˜์—ญ์œผ๋กœ ๋ถ„๋ฆฌ๋œ ๊ฒƒ์ด์—์š”.

 

 

         ๐Ÿ‘‰ ์‚ฌ์šฉ, ๊ตฌ์„ฑ์˜ ๋ถ„๋ฆฌ

 

 

         ๐Ÿ‘‰ ํ• ์ธ ์ •์ฑ…์˜ ๋ณ€๊ฒฝ

 

'FixDiscountPolicy' -> 'RateDiscountPolicy'๋กœ ๋ณ€๊ฒฝํ•ด๋„ ๊ตฌ์„ฑ ์˜์—ญ๋งŒ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฒƒ์ด๊ณ , ์‚ฌ์šฉ ์˜์—ญ์€ ์ „ํ˜€ ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ์žˆ์ง€ ์•Š์€ ๊ฒƒ์ด์—์š”.

 

 

         ๐Ÿ‘‰ ํ• ์ธ ์ •์ฑ… ๋ณ€๊ฒฝ ๊ตฌ์„ฑ ์ฝ”๋“œ - AppConfig

package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;

public class AppConfig {
    public MemberService memberService() {
        return new MemberServiceImpl(memberRepository());
	}
    
    public OrderService orderService() {
        return new OrderServiceImpl(
                memberRepository(),
                discountPolicy());
	}
    
    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();
	}
    
    public DiscountPolicy discountPolicy() {
//      return new FixDiscountPolicy();
        return new RateDiscountPolicy();
    }
}

 

'AppConfig'์—์„œ ํ• ์ธ ์ •์ฑ… ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ๊ตฌํ˜„์„ 'FixDiscountPolicy'์—์„œ 'RateDiscountPolicy' ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์ด์—์š”.

์ด์ œ ํ• ์ธ ์ •์ฑ…์„ ๋ณ€๊ฒฝํ•ด๋„, Application ๊ตฌ์„ฑ ์—ญํ•  ๋‹ด๋‹น์ธ AppConfig๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

๋”ฐ๋ผ์„œ Client Cod์ธ 'OrderServiceImpl'๋ฅผ ํฌํ•จํ•ด์„œ ์‚ฌ์šฉ ์˜์—ญ์˜ ์–ด๋–ค Code๋„ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ์ด์—์š”.

๋‹ค๋งŒ, ๊ตฌ์„ฑ ์˜์—ญ์€ ๋‹น์—ฐํžˆ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด์—์š”. ๊ตฌ์„ฑ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” AppConfig๋ฅผ Application์ด๋ผ๋Š” ๊ณต์—ฐ์˜ ๊ธฐํš์ž๋กœ ์ƒ๊ฐํ•˜๋ฉด ์‰ฌ์šด ๊ฒƒ์ด์—์š”. ๊ณต์—ฐ ๊ธฐํš์ž๋Š” ๊ณต์—ฐ ์ฐธ์—ฌ์ž์ธ ๊ตฌํ˜„ ๊ฐ์ฒด๋“ค์„ ๋ชจ๋‘ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒŒ ๋‹น์—ฐํ•˜๋‹ต๋‹ˆ๋‹ค!

 

 

 

 

์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ - ๊น€์˜ํ•œ ๊ฐ•์‚ฌ๋‹˜์˜ ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ ๊ฐ•์˜

์ด ๊ธ€์€ ๊ฐ•์˜ ๋‚ด์šฉ๊ณผ ๊ฐ•์˜ ์ž๋ฃŒ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ '์ฃผ๋‹ˆํ•˜๋ž‘'์ด '์ฃผ๋‹ˆํ•˜๋ž‘' ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ ๋‚ด์šฉ ์ž„์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

 

 





์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€ ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ช…๋ก์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•