<์ถœ์ฒ˜ ์ธํ”„๋Ÿฐ>

 

 

 

์„น์…˜ 3. ์˜ˆ์ œ๋งŒ๋“ค๊ธฐ์—์„œ ์ž ๊น ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ ๊ตฌํ˜„์ฒด์˜ ์˜์กด์„ฑ์„ ์™„์ „ํžˆ ๋ฐฐ์ œํ•œ ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ์—ˆ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ๊ตฌํ˜„์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ํ•˜๋‚˜ํ•˜๋‚˜ ํด๋ž˜์Šค๋ฅผ ์ฐพ์•„๊ฐ€ ์ˆ˜์ •ํ•ด์•ผํ•˜๋Š” ์ผ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์˜€๋‹ค. ์ด๋ฒˆ ๊ฐ•์˜์—์„œ๋Š” ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ๋ฐ”๊พธ๋ฉฐ ์Šคํ”„๋ง์˜ ์–ด๋–ค ์ ์ด ํŽธ๋ฆฌํ•˜์—ฌ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์„ ํƒํ–ˆ์ง€๋Š” ์•Œ์•„๊ฐ€๋Š” ๊ฐ•์˜๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์–ด๋–ค ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ์•ผ ์˜์กด์ ์ด์ง€ ์•Š๋Š” ์ฝ”๋“œ์ธ์ง€ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์ด ์ข‹์„ ๋“ฏ ํ•˜๋‹ค.

 

 

 


 

[ โ—€๏ธŽ ]

 

[Spring] ๊น€์˜ํ•œ ๊ธฐ๋ณธํŽธ_ ์„น์…˜ 3. ์˜ˆ์ œ๋งŒ๋“ค๊ธฐ

์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์˜ ๊ธฐ์ดˆ ํ‹€์„ ๋งŒ๋“œ๋Š” ๊ฐ•์˜์˜€๋‹ค. ์—ฌ๊ธฐ์—์„œ ์•Œ์•„๋‘˜ ๊ฒƒ์€ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์ฒด๋‹ค. ํ˜„์žฌ ๊ฐ•์˜ ์‹œ์ ์—์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ์„ ์™„๋ฒฝํ•˜๊ฒŒ ํ•ด๊ฒฐํ•˜์ง„

post-this.tistory.com

 

 

 

 

 

๐Ÿงธ 

์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์„ธ์š”


 

 

๐Ÿงธ ๊ตฌ์กฐํŒŒ์•… ๋ ˆ์ธ ๊ผฌ์šฐ- ๐Ÿงธ

  • member
    ใ„ด class : Member, MemberServiceImpl, MemoryMemberRepository
    ใ„ด interface : MemberService, MemberRepository
    ใ„ด enum : Grade
  • discount
    ใ„ด class : FixDiscountPolicy, RateDiscountPolicy
    ใ„ด interface : DiscountPolicy
  • order
    ใ„ด class : Order, OrderServiceimpl
    ใ„ด interface : OrderService
  • test
    main
    ใ„ด MemberApp, OrderApp
    test
    ใ„ด RateDiscountPolicyTest, MemberServiceTest
  • AppConfig

 


 

โœ”๏ธ  discount

    < ์กฐ๊ฑด >

  • ์„œ๋น„์Šค ์˜คํ”ˆ ์ง์ „์— ํ• ์ธ ์ •์ฑ…์„ ์ง€๊ธˆ์ฒ˜๋Ÿผ ๊ณ ์ • ๊ธˆ์•ก ํ• ์ธ์ด ์•„๋‹ˆ๋ผ ์ข€ ๋” ํ•ฉ๋ฆฌ์ ์ธ ์ฃผ๋ฌธ ๊ธˆ์•ก๋‹น ํ•  ์ธํ•˜๋Š” ์ •๋ฅ % ํ• ์ธ์œผ๋กœ ๋ณ€๊ฒฝ.
  • ๊ธฐ์กด ์ •์ฑ…์€ VIP๊ฐ€ 10000์›์„ ์ฃผ๋ฌธํ•˜๋“  20000์›์„ ์ฃผ๋ฌธํ•˜๋“  ํ•ญ์ƒ 1000์›์„ ํ• ์ธ.
  • ์ƒˆ๋กœ ๋‚˜์˜จ ์ •์ฑ…์€ 10%๋กœ ์ง€์ •ํ•ด๋‘๋ฉด ๊ณ ๊ฐ์ด 10000์› ์ฃผ๋ฌธ์‹œ 1000 ์›์„ ํ• ์ธํ•ด์ฃผ๊ณ , 20000์› ์ฃผ๋ฌธ์‹œ์— 2000์›์„ ํ• ์ธํ•ด์ฃผ๋Š” ๊ฑฐ์—์š”!

 

โ–บ RateDiscountPolicy

: ์ด ์ฃผ๋ฌธ ๊ธˆ์•ก์˜ 10% ํ• ์ธ ์ •์ฑ…

 

private int discountPercent =10;
.
.
    if (member.getGrade() == Grade.VIP) {
            return price * discountPercent / 100;
        }

 

์ •๋ฅ ํ• ์ธ์ •์ฑ…์ธ RateDiscountPolicy๋Š” DiscountPolicy ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ๋˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์ธ DiscountPolicy์— ์†ํ•ด์žˆ๋Š” discount ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จ์‹œ์ผœ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค. ๋ฉ”์„œ๋“œ discount๋ฅผ ์ œ๋Œ€๋กœ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  member์™€ price ๊ฐ’์ด ๋“ค์–ด์™€์•ผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋“ค์–ด์˜จ member์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ Member๋ฅผ ํ†ตํ•ด getGradeํ•˜์—ฌ VIP์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ๊ทธ ํ›„ ๋“ค์–ด์˜จ price๊ฐ’์„ ํ• ์ธ๋ฅ ์„ ๊ณ„์‚ฐํ•˜์—ฌ returnํ•œ๋‹ค.

 

 

 


 

 

โœ”๏ธ  Test

 

โ–บ RateDiscountTest

: % ํ• ์ธ ๊ด€๋ จ ํ…Œ์ŠคํŠธ 

 

 

ํ…Œ์ŠคํŠธ ๋งŒ๋“œ๋Š” ๋‹จ์ถ•๊ธฐ

mac : cmd + shift + T 

    @Test
    @DisplayName("VIP๋Š” 10% ํ• ์ธ์ด ์ ์šฉ๋˜์–ด์•ผํ•œ๋‹ค.")
    void vip_o() {

        Member member = new Member(1L, "memberVIP", Grade.VIP);

        int discount = discountPolicy.discount(member, 10000);
        
        assertThat(discount).isEqualTo(1000);

    }

 

 

@Test

: ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ์œ„์— ์„ ์–ธํ•ด์•ผ ํ…Œ์ŠคํŠธ ๋Œ€์ƒ์ด ๋œ๋‹ค.

 

 

assertThat(A).isEqualTo(B)

: A์™€ B์˜ ๊ฐ’์ด ๋งž๋Š”์ง€ ๋Œ€์ƒ ๋‚ด์šฉ ์ž์ฒด๋ฅผ ๋น„๊ตํ•˜๋Š” ํ…Œ์ŠคํŠธ๋‹ค.

 

 

@Test ํ•  ๋•Œ ์“ฐ๋Š” ๊ณต์‹

โžฐ  given (์‹คํ–‰๋‹จ๊ณ„) : ์–ด๋–ค ์กฐ๊ฑด์„ ์ฃผ๊ณ 
Member member = new Member(1L, "memberVIP", Grade.VIP);
โ–ป ID๋Š” 1L, ์ด๋ฆ„์€ memberVIP, ๋“ฑ๊ธ‰์€ VIP์ธ member๊ฐ€

โžฐ   when (์‹คํ–‰๋‹จ๊ณ„) : ์ƒํ™ฉ์ด ์–ด๋– ํ• ๋•Œ
int discount = discountPolicy.discount(member, 10000);
โ–ป ์ด ๊ธˆ์•ก ๋งŒ์›์„ ํ• ์ธ ์ •์ฑ…์— ๋„ฃ์—ˆ์„ ๋•Œ

โžฐ  then (๊ฒ€์ฆ๋‹จ๊ณ„) : ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
assertThat(discount).isEqualTo(1000);
โ–ป ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ 1000์ด ๋˜์–ด์•ผํ•œ๋‹ค.

 

 

 

••• ์ด์–ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

import static org.assertj.core.api.Assertions.*;

 

Assertions

: AssertJ์—์„œ ์ œ๊ณตํ•˜๋Š” API๋กœ ํ…Œ์ŠคํŠธ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค.

 

 

Assertions.assertThat(discount).isEqualTo(1000);

 

๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ฝ”๋“œ

mac: command + enter

assertThat(discount).isEqualTo(1000);

 

 

assertThat(A).isEqualTo(B)

: A์˜ ๊ฐ’์ด ๊ธฐ๋Œ“๊ฐ’ B์™€ ๊ฐ™์€์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.
ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” discount์˜ ๊ฐ’๊ณผ 1000์˜ ๊ฐ’์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

๋ฐ˜๋Œ€๋กœ ๋‹ค๋ฅธ์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋Š” assertThat(A).isNotEqualTo(B)์ด ๋˜๊ฒ ๋‹ค.

 

 

 

- ๋‹ค์–‘ํ•œ assertThat()์˜ ์ •๋ณด๋ฅผ ์ œ๊ณต, ์ถœ์ฒ˜

 

[Spring] Junit5 Assertions, assertThat()

์ด ๊ธ€์€ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ์•Œ๊ฒŒ ๋œ ๋‚ด์šฉ๋“ค์„ ๊ธฐ๋กํ•˜๋Š” ๊ธ€ ์ž…๋‹ˆ๋‹ค. ์˜ค๋ฅ˜๋‚˜ ๊ณ ์ณ์•ผ ํ•  ์‚ฌํ•ญ๋“ค์ด ์žˆ๋‹ค๋ฉด ์ง€์  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค! โ›…๏ธ Assertions ์ด์ „ ํฌ์ŠคํŠธ์—์„œ Junit5๋ฅผ ํ†ตํ•ด ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

m42-orion.tistory.com

 

 

 

 


 

 

 

โœ”๏ธ  AppConfig

 

OCP์™€ DIP๋ฅผ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐํ•  ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

์ฆ‰, ํ™˜๊ฒฝ๊ตฌ์„ฑ์„ ์ˆ˜ํ–‰ํ•  ํด๋ž˜์Šค์ด๋‹ค. ๋ณดํ†ต Ioc์ปจํ…Œ์ด๋„ˆ ํ˜น์€ DI์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

 

 

Q. ์›๋ž˜ ๋ฌธ์ œ์ ์€?


A.  OrderServiceImpl์ด FixDiscountPolicy์— ์˜์กดํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.

์ˆ˜์ • ์ „ ์ฝ”๋“œ

public class OrderServiceImpl implements OrderService {
     private final DiscountPolicy discountPolicy = new RateDiscountPolicy();
 }

 

 

ํ• ์ธ ์ •์ฑ…์ด ๋‹ฌ๋ผ์ง€๋ฉด OrderServiceImpl ํด๋ž˜์Šค์— ๊ฐ€์„œ RateDiscounPolicy๋ฅผ ๋‹ค๋ฅธ ํ• ์ธ ์ •์ฑ… ํด๋ž˜์Šค๋กœ ๋งค๋ฒˆ ์ˆ˜์ •ํ•ด์ค˜์•ผํ•œ๋‹ค.

๊ทธ๋ ‡๊ธฐ์— ์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์˜์กด์„ฑ ์ฃผ์ž…์œผ๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

 

์ˆ˜์ • ํ›„ ์ฝ”๋“œ

public class OrderServiceImpl implements OrderService{

   private final MemberRepository memberRepository;
   private final DiscountPolicy discountPolicy;

   public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
       this.memberRepository = memberRepository;
       this.discountPolicy = discountPolicy;
   }
}

 

private

: ์„ ์–ธ๋œ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ์•ˆํ•˜๋Š” ์ ‘๊ทผ์ œ์–ด์ž

 

final

: final์ด ๋ถ™์€ ๋ณ€์ˆ˜๋Š” ์ƒ์ˆ˜๊ฐ€ ๋˜์–ด, final์ด ๋ถ™์€ ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

 

Q. ์™œ ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ private final๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์„ ์–ธํ•˜๋‚˜?

A. private๋Š” ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งŒ์•ฝ private๋งŒ ์ผ๋‹ค๋ฉด ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ณ€์ˆ˜ ๋‚ด์šฉ์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์˜€์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ final์ด ๋ถ™์œผ๋ฉด์„œ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ๋Š” ๋‹จ ํ•œ ๋ฒˆ์˜ ์ดˆ๊ธฐํ™”๋งŒ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค. (ํ•œ ๋ฒˆ ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜๋ฉด ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.) ๊ทธ๋ž˜์„œ ํ•ด๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์™ธ๋ถ€ ์ ‘๊ทผ๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์žฌํ• ๋‹นํ•˜๋Š” ๊ฒƒ๋„ ๋ถˆ๊ฐ€๋Šฅํ•ด์ง„ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๋†“์น˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ์€ ํ•ด๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋Š” ์ ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์•„๋งˆ final์„ ํ†ตํ•ด ํ•˜๊ณ  ์‹ถ์—ˆ๋˜ ๊ฒƒ์€ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ๊ตฌํ˜„์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ผ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ํ• ์ธ์ •์ฑ…์ด VIP๋งŒ 10ํผ์„ผํŠธ ํ• ์ธ์œผ๋กœ ์ง€์ •ํ•ด๋†จ๋Š”๋ฐ, ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์‹ค์ˆ˜๋กœ ๋ชจ๋“  ๊ณ ๊ฐ์—๊ฒŒ 10ํผ์„ผํŠธ ํ• ์ธ์œผ๋กœ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ ํฐ ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐํ•˜๊ธฐ์— ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ ์ž(์ผ๊ด€์„ฑ ์œ ์ง€๋ฅผ ์œ„ํ•ด) ์ƒ์„ฑ์ž์— private final๋กœ ์„ ์–ธํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

 

••• ์ด์–ด์„œ AppConfig ์ฝ”๋“œ ๋ถ€๋ถ„ •••

 

 

 

 

๋ฆฌํŒฉํ„ฐ๋ง ํ›„ ์ฝ”๋“œ

@Configuration
public class AppConfig {

    @Bean
    public MemberService memberService() {
        return new MemberServiceImpl(memberRepository());
    }
.
.
.

    @Bean
    public DiscountPolicy discountPolicy() {
        return new RateDiscountPolicy();
    }

}

 

@Configuration

: ์„ค์ •ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜ or Bean์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ Bean์„ ๋“ฑ๋กํ•  ๋•Œ ์‹ฑ๊ธ€ํ†ค์ด ๋˜๋„๋ก ๋ณด์žฅํ•ด์ค€๋‹ค. ๋˜ ์Šคํ”„๋ง์ปจํ…Œ์ด๋„ˆ์—์„œ Bean์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

 

 

 

@Bean

: Spring์˜ DI Container์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” POJO(Plain Old Java Object)๋ฅผ Bean์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. @Bean์œผ๋กœ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ๋“ค์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์ •ํ•œ๋‹ค.

 

 

AppConfig์— ์ธํ„ฐํŽ˜์ด์Šค์— ๋งž๋Š” ๊ตฌํ˜„์ฒด๋ฅผ returnํ•ด์ค€๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ์–ด๋–ค ํด๋ž˜์Šค์—์„œ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ํ•„์š”๋กœ ํ•  ๋•Œ ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์ •์˜ํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

MemberServiceImpl(๊ตฌํ˜„์ฒด)์€ MemberService(์ธํ„ฐํŽ˜์ด์Šค)๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ธํ„ฐํŽ˜์ด์Šค์— ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ผญ ๋“ค์–ด๊ฐ€์•ผํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ตฌํ˜„์ฒด ์† return ๊ฐ’์„ ๋ณด๋ฉด save์™€ findById๋กœ ์ฃผ๊ณ ์žˆ๋Š”๋ฐ, ํšŒ์›๊ฐ€์ž…์ด ๋œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ˜น์€ ์ €์žฅ์†Œ์— ๋“ค์–ด๊ฐ€์žˆ๋Š” ํšŒ์› ์ •๋ณด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด MemberRepository ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ •๋ฆฌํ•ด๋ณด๋ฉด MemberService(์ธํ„ฐํŽ˜์ด์Šค) - MemberServiceImpl(๊ตฌํ˜„์ฒด)   ->   MemberRepository(์ธํ„ฐํŽ˜์ด์Šค) - MemoryMemberRepository(๊ตฌํ˜„์ฒด)๋กœ MemberServiceImpl์ด save์™€ findById๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด MemberRepository ์ธํ„ฐํŽ˜์ด์Šค์™€ ์—ฐ๊ฒฐํ–ˆ๊ณ  ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” MemberServiceImpl์„ ์ƒ์†ํ•˜๊ณ  ์žˆ๊ธฐ์— ๊ตฌํ˜„์ฒด์™€ ๊ตฌํ˜„์ฒด๊ฐ€ ์—ฐ๊ฒฐ๋œ ์ƒํ™ฉ์„ ๋ง‰์€ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ AppConfig๋ฅผ ๋‹ค์‹œ ๋ณด๋ฉด ๋ฉ”์„œ๋“œ memberService( )๋Š” MemberService ํƒ€์ž…์œผ๋กœ ๋นˆ์„ ๋“ฑ๋กํ–ˆ๊ณ  MemberServiceImpl๊ณผ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉฐ, ๊ทธ ์†์—์„œ save์™€ findById๋ฅผ ์ˆ˜ํ–‰์„ ์œ„ํ•ด MemberRepository๋ฅผ ์˜์กด์„ฑ์œผ๋กœ ๋ฐ›์•„ ํšŒ์› ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.

๊ทธ ์•„๋ž˜ DiscountPolicy๋ฅผ ๋ณด์ž. ์ „์˜ ๋ฌธ์ œ์ ์œผ๋กœ, OrderServiceImpl ํด๋ž˜์Šค์— ๋“ค์–ด๊ฐ€์„œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๊ฐ€ ๋ฐ”๋€ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ฒŒ ํ–ˆ๋‹ค.(FixDiscountPolicy๋ฅผ RateDiscountPolicy๋กœ) ๊ทธ๋Ÿฐ๋ฐ AppConfig์—์„œ ์ •์˜ํ•˜๋Š” ์—ญํ• ๋งŒ ์ˆ˜ํ–‰ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋‹ˆ OrderServiceImpl์—๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์ •์˜ํ•˜๊ณ  AppConfig์—์„œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์ฒด๋ฅผ ์—ฐ๊ฒฐ์ง€์–ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

- @Configuration ์ •๋ณด ์ถœ์ฒ˜

 

[Spring] @Configuration ๊ฐœ๋…๊ณผ ์žฅ์ 

๋”๋ณด๊ธฐ 1. @Configuration ์ด๋ž€? : @Configuration์ด๋ผ๊ณ  ํ•˜๋ฉด ์„ค์ •ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜ or Bean์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค. ๋”๋ณด๊ธฐ 2. @Configuration์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ญ๊ฐ€ ์ข‹์€๋ฐ? : @Configuration ์• 

castleone.tistory.com

 

 

 

- @Bean์— ๊ด€๋ จํ•œ ์–ด๋…ธํ…Œ์ด์…˜ ์ถœ์ฒ˜

 

[Spring] ๋นˆ ๋“ฑ๋ก์„ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜ @Bean, @Configuration, @Component ์ฐจ์ด ๋ฐ ๋น„๊ต - (1/2)

๊ธฐ์กด์˜ Spring MVC์—์„œ๋Š” xml์„ ํ™œ์šฉํ•˜์—ฌ Bean์„ ๋“ฑ๋กํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•˜๋Š” ์š”์†Œ๋“ค์„ xml์— ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด ์ƒ๋‹นํžˆ ๋ฒˆ๊ฑฐ๋กœ์›Œ ์ ธ์„œ ์–ด๋…ธํ…Œ์ด์…˜(Annotation, @)๋ฅผ ํ™œ

mangkyu.tistory.com

 

 

 

 

โœ”๏ธ  MemberServiceTest

์„น์…˜ 3์—์„œ ๋งŒ๋“ค์—ˆ๋˜ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ˆ˜์ •ํ•˜๊ธฐ.

 

     @BeforeEach
     public void beforeEach() {
         AppConfig appConfig = new AppConfig();
         memberService = appConfig.memberService();
     }

 

@BeforeEach

: ํ…Œ์ŠคํŠธ๊ฐ€ ๋ณต์žกํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋ฉฐ ํ…Œ์ŠคํŠธ ์ดˆ๊ธฐํ™”๋ฅผ ์œ„ํ•ด @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, @TestTemplate ๊ฐ€ ๋ถ™์€ ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ „์— ์‹คํ–‰๋œ๋‹ค.

 

ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์‚ฌ์šฉํ•  MemberService๋ฅผ ์„ ์–ธํ•˜๊ณ  ํ…Œ์ŠคํŠธ์— ๋“ค์–ด๊ฐ„๋‹ค. AppConfig์— ์ •์˜๋˜์–ด์žˆ๋Š” memberService ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด AppConfig๋ฅผ ์ •์˜ํ•ด์คฌ๋‹ค. ๊ทธ๋ฆฌ๊ณ  memberService(์ธํ„ฐํŽ˜์ด์Šค)์™€ ์—ฐ๊ฒฐ๋œ ๊ตฌํ˜„์ฒด๋ฅผ ์ œ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์˜ค๊ธฐ ์œ„ํ•ด์„œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ค€๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ํ›„์— ์žˆ๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ ์—ฐ๊ฒฐ๋œ ๊ตฌํ˜„์ฒด์— ๋งž์ถฐ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

 

- @BeforeEach ์ถœ์ฒ˜

 

[Spring] @Before @BeforeClass @BeforeEach @BeforeAll

@Before @BeforeClass @BeforeEach @BeforeAll Spring ์—์„œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ „์— ๋ฐ˜๋ณต์ ์œผ๋กœ ํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž ์ธ์ฆ์ด ์„ ํ–‰๋˜์–ด์•ผ ํ•˜

bcp0109.tistory.com

 

 

 

 


 

 

 

โœ”๏ธ  OrderApp

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๊ธฐ

 

 

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
MemberService memberService = applicationContext.getBean("memberService", MemberService.class);
OrderService orderService = applicationContext.getBean("orderService", OrderService.class);

 

AnnotationConfigApplicationContext

: java ๊ธฐ๋ฐ˜์˜ ์„ค์ •ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Bean ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

AnnotationConfigApplicationContext์— ํ•ด๋‹น ํ…Œ์ŠคํŠธ์— ํ•„์š”ํ•œ Bean์ด ๋“ค์–ด์žˆ๋Š” ์„ค์ •ํด๋ž˜์Šค๋ฅผ ๊ด„ํ˜ธ ์•ˆ์— ๋„ฃ์–ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด Bean์„ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.(@Bean์ด ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ๋“ค์„ ๊ด€๋ฆฌํ•จ) ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๊ทธ ์•„๋ž˜ ์ค„์— ์žˆ๋Š” ์ฝ”๋“œ์ฒ˜๋Ÿผ getBean์„ ํ†ตํ•ด ์„ค์ •ํด๋ž˜์Šค์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค. (๋งŒ์•ฝ @Bean์ด ์—†๋‹ค๋ฉด ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†๋‹ค)

์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์•Œ์•„๋ณด์ž. ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋„ฃ์—ˆ์–ด๋„ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„  ์„ค์ •ํด๋ž˜์Šค ์† ์–ด๋–ค ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์•Œ๋ ค์ค˜์•ผํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— "memberService"์™€ "orderService"์ธ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ ์–ด getBean์„ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋‹ค.

๋งจ ์•ž์— MemberService์™€ OrderService๊ฐ€ ๋ถ™์–ด์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํ˜„์žฌ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋‚ด์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฆ„์„ ๊ฐ๊ฐ memberService, orderService๋ฅผ ๋ถ™์—ฌ์คฌ๋Š”๋ฐ ๊ทธ ์ด๋ฆ„๋“ค์˜ (๊ฐ€์ ธ์˜ฌ) ํƒ€์ž…์„ ์•ž์— ์ •์˜ํ•œ ๊ฒƒ์ด MemberService์™€ OrderService๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ๊ฐ ํƒ€์ž…์˜ ๋งž๊ฒŒ ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜จ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๋’ค์— ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค. MemberService.class์™€ OrderService.class์™€ ๊ฐ™์ด ๋ง์ด๋‹ค. ๋งŒ์ผ  memberService ๋ฉ”์„œ๋“œ๋ฅผ ์„ค์ •ํด๋ž˜์Šค ๋‚ด์— MemberService.class๋กœ ์ง€์ •์„ ํ•ด๋†จ๋Š”๋ฐ ํ…Œ์ŠคํŠธ์—์„œ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ ๋ฐ”๊ฟ€ ๊ฒฝ์šฐ ์‹ค์ œ ๋นˆ๊ณผ ๊ธฐ๋Œ€ํ•œ ๋นˆ์˜ ํƒ€์ž…์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒฐ๊ณผ ๊ฐ’์ด ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  .class๋Š” ์•Œ๋งž๊ฒŒ ์ ์–ด๋†จ์ง€๋งŒ ๋งจ ์•ž์— MemberService์™€ OrderService๋ฅผ ๋‹ค๋ฅธ ํƒ€์ž…์„ ์ ์„ ๊ฒฝ์šฐ์—๋„ ํƒ€์ž…์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค.

 

 

 

 

- ApplicationContext ์ถœ์ฒ˜

 

[Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(ApplicationContext)

ApplicationContext๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•œ๋‹ค.(์ •ํ™•ํžˆ๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ถ€๋ฅผ ๋•Œ, BeanFactory, ApplicationContext๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ๋งํ•˜์ง€๋งŒ, BeanFactory๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†๋‹ค.)

velog.io

 

 

 

 


 

 

[ โ–ถ๏ธŽ ]

 

 

 

[Spring] ๊น€์˜ํ•œ ๊ธฐ๋ณธํŽธ_ ์„น์…˜ 5. ์Šคํ”„๋ง์ปจํ…Œ์ด๋„ˆ์™€์Šคํ”„๋ง๋นˆ

์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ๋นˆ ์ด๋ฆ„๊ณผ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜์—ฌ ๊ด€๋ฆฌํ•ด์ค€๋‹ค. Public๊ณผ ํ•จ๊ป˜ ์“ด ๊ฒƒ์€ ํƒ€์ž…๊ณผ ์ด๋ฆ„์ด ๋œ๋‹ค. ์˜ˆ์‹œ๋กœ public MemberService memberService( )์˜ ๊ฒฝ์šฐ MemberService๋Š” ํƒ€

post-this.tistory.com

 

 

 

 

ํ˜ผ์ž ์ฒœ์ฒœํžˆ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์ ํ˜€์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ, ๊ฐ์•ˆํ•˜๊ณ  ๋ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ€ ๋ชจ๋‘๋“ค ํ™”์ดํŒ…ํ•˜์„ธ์š” ๐Ÿ€

 

+ Recent posts