EEALL@ONCE

๐ŸŒฑ AppConfig ๋ฆฌํŒฉํ„ฐ๋ง ๋ณธ๋ฌธ

Spring๐ŸŒฑ

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

์˜ฌ์—ฃ์›์Šค 2023. 7. 21. 08:37
728x90

์ค‘๋ณต์„ ์—†์• ๊ณ , ์—ญํ• ์— ๋”ฐ๋ฅธ ๊ตฌํ˜„์ด ํ™•์‹คํ•ด์•ผํ•œ๋‹ค. 

๋ฆฌํŽ™ํ„ฐ๋ง ํ•  ๋•Œ ์ง€์ผœ์•ผํ•˜๋Š” ์ : 

1. ๊ตฌ์„ฑ ์ •๋ณด์—์„œ ์—ญํ• ๊ณผ ๊ตฌํ˜„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌ 

2. ์ค‘๋ณต ์ œ๊ฑฐ 

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

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

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

 

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

public class AppConfig {
    /* ๊ฐ์ž ๋ถ€๋ถ„์„ ์ •ํ™•ํžˆ ๋‚˜๋ˆ” */

    
    public MemberRepository memberRepository(){
        return new MemoryMemberRepository();
    }
    
    public DiscountPolicy discountPolicy(){
        return new FixDiscountPolicy();
    };

    public MemberService memberService() {
        return new MemberServiceImpl(memberRepository());
    }
    
    public OrderService orderService(){
        return new OrderServiceImpl(memberRepository(),discountPolicy());
    }
    

}

๊ตฌํ˜„์ฒด๋ฅผ ํ•œ ๊ณณ์—์„œ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋ชจ๋‘ ์ˆ˜์ • ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฐ”๊ฟจ๋‹ค.

 

728x90