๐Ÿ–ฅ์˜์กด๊ด€๊ณ„ ์ž๋™ ์ฃผ์ž…โ—๏ธ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป

DEVJUNยท2021๋…„ 12์›” 23์ผ
1
post-thumbnail
post-custom-banner

์•ž์„œ ์ด์•ผ๊ธฐํ•œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…(DI)๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 4๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ฃผ์ž… ๋ฐฉ๋ฒ•, ์ˆ˜์ •์ž(Setter๋ฅผ ํ†ตํ•ด์„œ) ์ฃผ์ž… ๋ฐฉ๋ฒ•, ํ•„๋“œ์— ์ง์ ‘ ์ฃผ์ž…(๋ณ€์ˆ˜ ํ•„๋“œ์— ์ง์ ‘ @Autowired ๋ถ™์ž„) ๋ฐฉ๋ฒ•, ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•œ ์ฃผ์ž… ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค!

์šฐ์„  ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•˜์ž๋ฉด~~

์ˆ˜์ •์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด set??? ๋ฉ”์„œ๋“œ๋ฅผ public์œผ๋กœ ์—ด์–ด ๋‘์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ˆ„๊ตฐ๊ฐ€ ์‹ค์ˆ˜๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ธฐ์— ์ ˆ๋Œ€ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๊ณ  ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณค ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.


ํ•„๋“œ ์ฃผ์ž…์€ ํ•„๋“œ๋ช…์—๋งŒ @Autowired๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ง ๊ฐ„๊ฒฐํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๋“ค์„ ์œ ํ˜นํ•˜์ง€๋งŒ ์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์„œ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ ํž˜๋“ค๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉํ•˜์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค.


โญ๏ธ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ๋ฒ•์ด๋‹ค.
์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ๋ฒ•์ด ์ฃผ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ์ž ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋งŒ ๋‹ค๋ค„๋ณด๋ ค ํ•œ๋‹ค.

์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ๋ฒ•์€ ์ง€๊ธˆ๊นŒ์ง€ ์•ž์—์„œ ๋ณด์•˜๋˜ ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

ํŠน์ง•

  • ์ƒ์„ฑ์ž ํ˜ธ์ถœ ์‹œ์ ์— ๋”ฑ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ(๋ถˆ๋ณ€์„ฑ)๋˜๋Š” ๊ฒƒ์ด ๋ณด์žฅ๋œ๋‹ค
  • ๋ถˆ๋ณ€, ํ•„์ˆ˜ ์˜์กด ๊ด€๊ณ„์— ์‚ฌ์šฉ

  @Component
  public class OrderServiceImpl implements OrderService {
      private final MemberRepository memberRepository;
      private final DiscountPolicy discountPolicy;
      
      @Autowired
      public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
          this.memberRepository = memberRepository;
          this.discountPolicy = discountPolicy;
      }
}

์ด ๋ฐฉ๋ฒ•์ด ์ค‘์š”ํ•œ ์ด์œ ๋Š” ๋ถˆ๋ณ€, ํ•„์ˆ˜์— ์žˆ๋‹ค.

๋“œ๋ผ๋งˆ๋ผ๋Š” ์†Œ์žฌ๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ๋‹ค. ๋“œ๋ผ๋งˆ๋Š” ๋ฐฐ์šฐ๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•ด๋†“๊ณ  ์ดฌ์˜์— ๋“ค์–ด๊ฐ€๊ณ , ๋ฐฉ์˜๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๋ฐฐ์šฐ๊ฐ€ ์ค‘๊ฐ„์— ์ฃผ์—ฐ ๋ฐฐ์šฐ์—๊ฒŒ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ์„œ ๋ฐฐ์šฐ๊ฐ€ ๊ต์ฒด๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž....๐Ÿคญ ์‹œ์ฒญ์ž ์ž…์žฅ์—์„  ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ๊ฒƒ์ด๋‹ค.

โญ๏ธ ๐Ÿ“๋Œ€๋ถ€๋ถ„์˜ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์€ ํ•œ๋ฒˆ ์ผ์–ด๋‚˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ์‹œ์ ๊นŒ์ง€ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ณ€๊ฒฝํ•  ์ผ์ด ์—†๋‹ค. ์˜คํžˆ๋ ค ๋Œ€๋ถ€๋ถ„์˜ ์˜์กด๊ด€๊ณ„๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ์ „๊นŒ์ง€ ๋ณ€ํ•˜๋ฉด ์•ˆ๋œ๋‹ค. (๋ถˆ๋ณ€ํ•ด์•ผ ํ•œ๋‹ค!)โ—๏ธโ—๏ธ

๐Ÿ‘‰๐Ÿป์ƒ์„ฑ์ž ์ฃผ์ž…์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋”ฑ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ์ดํ›„์— ํ˜ธ์ถœ๋˜๋Š” ์ผ์ด ์—†๋‹ค ๋”ฐ๋ผ์„œ ๋ถˆ๋ณ€ํ•˜๊ฒŒ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค



@Component
    public class OrderServiceImpl implements OrderService {
        private final MemberRepository memberRepository;
        private final DiscountPolicy discountPolicy;
	
    	@Autowired
        public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicydiscountPolicy) {
            this.memberRepository = memberRepository;
            //this.discountPolicy = discountPolicy
        }
}

์œ„์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด discountPolicy์— ๋Œ€ํ•ด ๊ฐ’์„ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ฃผ์„์œผ๋กœ ๋ˆ„๋ฝ ์‹œ์ผฐ๋‹ค. ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ฃผ์ž… ๋ฐฉ๋ฒ•์—์„œ๋Š” ํ•„๋“œ์— final ํ‚ค์›Œ๋“ค ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ์„ค์ •๋˜์ง€ ์•Š๋Š” ์˜ค๋ฅ˜๋ฅผ ์ปดํŒŒ์ผ ์‹œ์ ์— ๋ง‰์•„์ค€๋‹ค๐Ÿ‘๐Ÿป๐Ÿ‘๐Ÿป

๐Ÿ“ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜์ด๋‹ค!! ๋‚˜๋Š” ์ฒ˜์Œ์— ๋ชจ๋“  ์˜ค๋ฅ˜๊ฐ€ ๋‹ค ์•ˆ์ข‹์€ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ์ ์ธ ๋Ÿฐํƒ€์ž„ ์ „์— ๋‚˜๋Š” ์—๋Ÿฌ์ด๋ฏ€๋กœ ์„ธ์ƒ์—์„œ ๊ฐ€์žฅ ๋น ๋ฅด๊ณ , ๊ฐ€์žฅ ์ข‹์€ ์˜ค๋ฅ˜์ด๋‹ค!!

์ถœ์ฒ˜

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8/dashboard

profile
๐Ÿง‘๐Ÿปโ€๐Ÿ’ปiOS
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€