ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SpringBoot] ์Šคํ”„๋ง Bean๊ณผ ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection) feat: ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด, ์ œ์–ด์˜ ์—ญ์ „
    ๐Ÿ’ปProgramming/SpringBoot 2024. 3. 12. 21:36

    Bean

    ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์†Œํ”„ํŠธ์›จ์–ด ์ปดํฌ๋„ŒํŠธ(component)๋กœ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋œปํ•œ๋‹ค. 

    ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ž€?

    ๋ณดํ†ต ์Šคํ”„๋ง์—์„œ๋Š” controller ํด๋ž˜์Šค์—์„œ ์š”์ฒญ์„ ๋ฐ›๊ณ , ํ•ด๋‹น ์š”์ฒญ์— ๋Œ€ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” service ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•˜๊ณ , ์ด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์—๋Š” repository ํด๋ž˜์Šค์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ controller๋Š” service์— ์˜์กดํ•˜๊ณ , service๋Š” repository์— ์˜์กดํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๊ฐ€ ๊ตฌ์„ฑ๋œ๋‹ค. 

    ์˜ˆ๋ฅผ ๋“ค์–ด,

    public class MemberController {
    
        private final MemberService memberService;
        
        public MemberController() {
        	this.memberService = new MemberService();
        }
    }

    ์ด ์ฝ”๋“œ์—์„œ MemberController๋Š” MemberService์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค. ์ด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์€ ๋ฌด์—‡์ผ๊นŒ?

    - ๋‘ "ํด๋ž˜์Šค"๊ฐ€ ๊ฐ•ํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ๋‹ค.

    - ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๋งบ์–ด์ ธ์žˆ๋‹ค.

     

    ์ด ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ? 

    ์™ธ๋ถ€์—์„œ MemberService๋ฅผ ์ฃผ์ž…(Injection)๋ฐ›์•„์•ผ ํ•œ๋‹ค.

    public class MemberController {
    
        private final MemberService memberService;
    
        public MemberController(MemberService memberService) {
            this.memberService = memberService;
        }
    }

     

    ์™€ ๊ฐ™์ด memberService๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ MemberController ๊ฐ์ฒด๋กœ ์ฃผ์ž…ํ•œ๋‹ค. 

    ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” Spring์ด๋ผ๋Š” DI(Dependency Injection, ์˜์กด์„ฑ ์ฃผ์ž…) ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹คํ–‰ ์‹œ์ ์— ํ•„์š”ํ•œ ๊ฐ์ฒด=๋นˆ์„ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋ฉฐ, ์˜์กด์„ฑ์ด ์žˆ๋Š” ๋‘ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ํ•œ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์ฃผ์ž…์‹œ์ผœ์•ผ ํ•œ๋‹ค. 

     

    ์Šคํ”„๋ง์—์„  ์ƒ์„ฑ์ž์— @Autowired๊ฐ€ ์กด์žฌํ•˜๋ฉด,์Šคํ”„๋ง์ด ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ฐพ์•„์„œ ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ•œ๋‹ค. ๋ฐ”๋กœ ์œ„์˜ ์ฝ”๋“œ์—์„  ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ•ด์ค€ ๊ฒƒ์ด๊ณ , @Autowired๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šคํ”„๋ง์—์„œ ์ž๋™์œผ๋กœ ๊ฐ์ฒด์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๋„ฃ์–ด์ค€๋‹ค. ํ•˜์ง€๋งŒ ์ด ์˜์กด์„ฑ์„ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด๋„ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋˜์–ด์žˆ์–ด์•ผ ์˜์กด์„ฑ์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, 

     @Controller
     public class MemberController {
         private final MemberService memberService;
         @Autowired
         public MemberController(MemberService memberService) {
             this.memberService = memberService;
         }
    }

    MemberService ํด๋ž˜์Šค๋„ ๋นˆ์œผ๋กœ ๋“ฑ๋ก, MemberController๋„ ๋ฌผ๋ก  ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์Šคํ”„๋ง์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋นˆ=๊ฐ์ฒด๋“ค์„ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋˜ํ•œ ์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง์ด ์ž๋™์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ์ฒด๋“ค์˜ ์˜์กด ๊ด€๊ณ„๊นŒ์ง€ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ฐœ๋…์€ "์ œ์–ด์˜ ์—ญ์ „(Inversion of Control, IoC)"์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฐ์ฒด์˜ ๊ด€๊ณ„๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ , ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•ด์คŒ์œผ๋กœ์จ ์ œ์–ด๊ฐ€ ์—ญ์ „๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

    ์˜์กด์„ฑ ์ฃผ์ž… ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

    1. ํ•„๋“œ ์ฃผ์ž…(๊ถŒ์žฅ x)

    @Controller
    public class MemberController {
    
        @Autowired MemberService memberService;
    
    }

    ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜์ง€๋งŒ, ์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์„œ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ํž˜๋“ค๋‹ค๋Š” ์น˜๋ช…์ ์ธ ๋‹จ์  ์กด์žฌ.

    DI ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์—†์œผ๋ฉด ์–ด๋–ค ๊ฒƒ๋„ ๋ถˆ๊ฐ€๋Šฅ

    2. setter ์ฃผ์ž…

    @Controller
    public class MemberController {
    
        private MemberService memberService;
    
        @Autowired
        public void setMemberService(MemberService memberService) {
            this.memberService = memberService;
        }
    
    }

    ๋‹จ์ : public์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผํ•˜๊ณ  ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ. ์˜์กด ๊ด€๊ณ„๋Š” ํ”„๋กœ์ ํŠธ ์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ถˆ๋ณ€ํ•˜์—ฌ์•ผ ๋งž๋‹ค. ๋‚˜์ค‘์— ๋ฐ”๊ฟ€์ผ์ด ์—†๋„๋ก ์„ค๊ณ„ํ•˜์—ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

    3. ์ƒ์„ฑ์ž ์ฃผ์ž…(๊ถŒ์žฅ)

    @Controller
    public class MemberController {
    
        private MemberService memberService;
    
        @Autowired
        public MemberController(MemberService memberService) {
            this.memberService = memberService;
        }
    
    }

    ์žฅ์ : ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์—๋งŒ ์ƒ์„ฑ์ด ๋˜๊ณ , ์ดํ›„ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ.

    • ๋ถˆ๋ณ€, ํ•„์ˆ˜์ธ ์˜์กด ๊ด€๊ณ„์— ์‚ฌ์šฉ๋œ๋‹ค.
    • ํ•„๋“œ๋ฅผ final๋กœ ์„ ์–ธํ•˜์—ฌ ์ˆ˜์ • ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.  -> ์ƒ์„ฑ์ž์—์„œ ํ˜น์‹œ๋ผ๋„ ๊ฐ’์ด ์„ค์ •๋˜์ง€ ์•Š๋Š” ์˜ค๋ฅ˜๋ฅผ  "์ปดํŒŒ์ผ ์‹œ์ "์— ๋ง‰์•„์ค€๋‹ค. 
    • ์œ„์—์„œ ๋งํ–ˆ๋“ฏ์ด ๋Œ€๋ถ€๋ถ„์˜ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์€ ํ•œ ๋ฒˆ ์ผ์–ด๋‚˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ข…๋ฃŒ ์‹œ์ ๊นŒ์ง€ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ณ€๊ฒฝํ•  ์ผ์ด ์—†์–ด์•ผ ํ•œ๋‹ค. -> ๋ถˆ๋ณ€ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์Šคํ”„๋ง์—์„œ๋Š” ์ด ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์„ ๊ถŒ์žฅํ•˜๋Š” ๊ฒƒ.!!
    • *์ฐธ๊ณ : ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ†ตํ•ด ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•  ๊ฒฝ์šฐ ์ƒ์„ฑ์ž๊ฐ€ ํด๋ž˜์Šค์— ํ•œ๊ฐœ๋งŒ ์žˆ๋‹ค๋ฉด ์Šคํ”„๋ง์—์„œ ์ž๋™์œผ๋กœ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ค€๋‹ค. 
    • ๋˜ํ•œ, ํ…Œ์ŠคํŠธ์‹œ ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ”๊ฟ” ๋ผ์›Œ๊ฐ€๋ฉฐ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅ
    @Test
    void createOrder() {
        MemoryMemberRepository memberRepository = new MemoryMemberRepository();
        memberRepository.save(new Member(1L, "name", Grade.VIP));
    
        OrderServiceImpl orderService = new OrderServiceImpl(memberRepository, new FixDiscountPolicy());
        Order order = orderService.createOrder(1L, "itemA", 10000);
        Assertions.assertThat(order.getDiscountPrice()).isEqualTo(1000);
    }

    ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ๊น”๋”ํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋ญ๊ฐ€ ์žˆ์„๊นŒ?

    @Component
    @RequiredArgsConstructor    // ์ƒ์„ฑ์ž ์ž๋™ ์ƒ์„ฑ
    public class OrderServiceImpl implements OrderService {
    
    	private final MemberRepository memberRepository;
        private final DiscountPolicy discountPolicy;
    
        @Override
        public Order createOrder(Long memberId, String itemName, int itemPrice) {
            Member member = memberRepository.findById(memberId);
            int discountPrice = discountPolicy.discount(member, itemPrice);
    
            return new Order(memberId, itemName, itemPrice, discountPrice);
        }
    
        public MemberRepository getMemberRepository() {
            return memberRepository;
        }
    }

    ์™€ ๊ฐ™์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉด ๋งค์šฐ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ†ตํ•œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

    @RequiredArgsConstructor

    ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. "final์ด ๋ถ™์€ ํ•„๋“œ"๋ฅผ ๋ชจ์•„์„œ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. 

    ์œ„ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž๋Š” 1๊ฐœ์ด๋‹ˆ ์ž๋™์œผ๋กœ  @Autowired๊ฐ€ ๋œ๋‹ค. 

    ์ตœ๊ทผ์—๋Š” ์ƒ์„ฑ์ž๋ฅผ 1๊ฐœ ๋‘๊ณ , @Autowired๋ฅผ ์ƒ๋žตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์œ„ ์–ด๋…ธํ…Œ์ด์…˜๊นŒ์ง€ ์‚ฌ์šฉํ•˜๋ฉด, ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ œ๊ณตํ•˜๋ฉฐ ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค. 

    ์–ด๋–ป๊ฒŒ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ํด๋ž˜์Šค๋“ค์„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์„๊นŒ?

    1. ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์ž๋™ ์˜์กด๊ด€๊ณ„ ์„ค์ •

    ์šฐ์„  ์œ„์˜ ์ฝ”๋“œ์—์„  @Controller์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด MemberController๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

    ๋ณดํ†ต @Component์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋นˆ์„ ๋“ฑ๋กํ•ด ์ฃผ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ @Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ๋นˆ์ด ๋“ฑ๋ก๋  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฑธ๊นŒ?

     

    @Controller ์–ด๋…ธํ…Œ์ด์…˜์— ๋“ค์–ด๊ฐ€ ๋ณด๋ฉด, ์ด๋ ‡๊ฒŒ ์ปดํฌ๋„ŒํŠธ ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค. @Service, @Repository ์–ด๋…ธํ…Œ์ด์…˜๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง์€ "์ปดํฌ๋„ŒํŠธ ์Šค์บ”"์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ๊ฐ์ฒด๋“ค์„ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ , ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค. 

    2. ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ง์ ‘ ์Šคํ”„๋ง ๋นˆ ๋“ฑ๋กํ•˜๊ธฐ

    @Configuration
     public class SpringConfig {
         @Bean
         public MemberService memberService() {
             return new MemberService(memberRepository());
         }
         @Bean
         public MemberRepository memberRepository() {
             return new MemoryMemberRepository();
         }
    }

    ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ญ์ œํ•˜๊ณ  ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

     

    ์Šคํ”„๋ง์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ, ๊ธฐ๋ณธ์œผ๋กœ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

    * @Bean๊ณผ @Component์˜ ์ฐจ์ด์ ??

    ๊ฐ‘์ž๊ธฐ ๊ทธ๋Ÿฌ๋ฉด @Bean๊ณผ @Component๋Š” ๋ฌด์—‡์ผ๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ฐพ์•„๋ณด๋‹ˆ ๊น€์˜ํ•œ๋‹˜์ด ์นœ์ ˆํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์…จ๋‹ค. 

    • @Bean์€ ์ˆ˜๋™์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ
    • @Component๋Š” ์ž๋™์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๊ธฐ๋Šฅ. 

    ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฐ”๋กœ ์œ„ ์ฝ”๋“œ์˜ @Configuration๋„ ๋‚ด๋ถ€์— @Component๊ฐ€ ๋ถ™์–ด์žˆ์–ด, ์„ค์ • ์ •๋ณด ์ž์ฒด๋„ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋ก ๋œ๋‹ค. 

    ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(Singleton Pattern)์ด๋ž€?

    ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์˜ค์ง 1๊ฐœ๋งŒ ์ƒ์„ฑ๋˜๋Š” ํŒจํ„ด์œผ๋กœ, ์ƒ์„ฑ์ž๊ฐ€ ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ํ˜ธ์ถœ๋˜๋”๋ผ๋„ ์‹ค์ œ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋Š” ํ•˜๋‚˜์ด๋ฉฐ ์ดํ›„์— ํ˜ธ์ถœ๋œ ์ƒ์„ฑ์ž๋Š” ์ตœ์ดˆ์˜ ์ƒ์„ฑ์ž๊ฐ€ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ. (=๊ณต์œ ํ•˜๋Š” ๊ฒƒ.) ์˜ˆ๋ฅผ ๋“ค์–ด OrderService์—์„œ MemberRepository๊ฐ€ ํ•„์š”ํ•˜์—ฌ ์˜์กด ๊ด€๊ณ„๊ฐ€ ์„ค์ •๋˜์–ด์žˆ์–ด๋„, MemberService์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” MemberRepository๊ฐ์ฒด๋ฅผ ๋‘˜์ด ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.(์ƒˆ๋กœ์šด MemberRepository ๊ฐ์ฒด ์ƒ์„ฑ ๋˜๋Š” ๊ฒƒ x)

    ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

    ์„œ๋ฒ„์˜ ์„ฑ๋Šฅ์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๋นˆ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์š”์ฒญ ๋งˆ๋‹ค ์ƒ์„ฑํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

    ์š”์ฒญ ํ•œ๊ฐœ๋‹น 5๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๋‹น 500๊ฐœ์˜ ์š”์ฒญ์ด ์˜ฌ ๊ฒฝ์šฐ ๊ฐ์ฒด๋Š” 2500๊ฐœ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์„œ๋ฒ„์˜ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋œ๋‹ค.

    ํ•˜์ง€๋งŒ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ bean์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด, was์‹œ์ž‘์‹œ์—๋งŒ ์ƒ์„ฑ์ด ๋˜๊ณ , ์š”์ฒญ์ด ๊ณ„์† ๋“ค์–ด์˜ค๋”๋ผ๋„ bean์€ ๋”์ด์ƒ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

    References

    https://mangkyu.tistory.com/150

     

    [Spring] ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection, DI)์ด๋ž€? ๋ฐ Spring์ด ์˜์กด์„ฑ ์ฃผ์ž…์„ ์ง€์›ํ•˜๋Š” ์ด์œ 

    1. ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection)์˜ ๊ฐœ๋…๊ณผ ํ•„์š”์„ฑ [ ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection) ์ด๋ž€? ] Spring ํ”„๋ ˆ์ž„์›Œํฌ๋Š” 3๊ฐ€์ง€ ํ•ต์‹ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์„ ์ง€์›ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๊ทธ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Inj

    mangkyu.tistory.com

    ์ธํ”„๋Ÿฐ - ์Šคํ”„๋ง๊ฐ•์˜(๊น€์˜ํ•œ๋‹˜)

Designed by Tistory.