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

 

์ฝ”๋กœ๋‚˜์— ๊ฑธ๋ ค ์ผ์ฃผ์ผ์€ ๋ˆ„์›Œ์žˆ๋‹ค ์‚ด์•„๋‚ฌ๋‹ค. ๋จน๊ณ  ์ž๊ณ ๋งŒ ๋ฐ˜๋ณตํ•˜๋‹ˆ ๋‚˜ํƒœํ•จ์˜ ๋ํŒ์™•์ด ๋˜์–ด๋ฒ„๋ ธ๋Š”๋ฐ... ์ด๋ ‡๊ฒŒ ๋‹ค์‹œ ๊ณต๋ถ€๋ฅผ ํ•˜๋ ค๋‹ˆ ๋ฐฐ๋กœ ํž˜๋“  ๊ฒƒ ๊ฐ™๋‹ค. 8์›”์—” ๊ผญ ๊ฐ•์˜๋ฅผ ๋‹ค ๋“ฃ๊ฒ ๋‹ค๊ณ  ๋‹ค์งํ–ˆ์—ˆ๋Š”๋ฐ,,, ์–ผ๋งˆ ๋‚จ์ง€ ์•Š์€ ์‹œ๊ฐ„ ๋‹ค๋ฅธ ๊ณต๋ถ€ ์ œ์ฒ˜๋‘๊ณ  8์›” ์•ˆ์— ๋๋‚ด๋ณผ๊นŒ ํ•œ๋‹ค. (๊ณผ์—ฐ ํ•  ์ˆ˜ ์žˆ์œผ๋ ค๋‚˜?) ์•„๋ฌดํŠผ ๋ชจ๋‘๋“ค ํ™”์ดํŒ…!!!!



๐Ÿงธ 

 


 

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

 • order

ใ„ด class : OrderServiceImpl

 • annotation

ใ„ด interface : MainDiscountPolicy

 • HelloLombok

 

 • AllBeanTest

ใ„ด class : AllBeanTest

 • AutoWiredTest

ใ„ด class : AutoWiredTest


 

โœ”๏ธ  OrderServiceImpl

 

โ–บ  ์ƒ์„ฑ์ž ์ฃผ์ž…

์ง€๊ธˆ๊นŒ์ง€ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์ด ๋˜๊ฒ ๋‹ค.

 

private final MemberRepository memberRepository;
private final DiscountPolicy discountPolicy;

     

final

: ์ดˆ๊ธฐํ™”๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค. 

 

 

final์„ ํ†ตํ•ด ์ƒ์„ฑ์ž์— ๊ฐ’์ด ์ œ๋Œ€๋กœ ์„ค์ •๋˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ์ž์— ๊ฐ’์„ ๋นผ๋จน์€ ๊ฑธ ๋น ๋ฅด๊ฒŒ ์•Œ์•„์ฐจ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœํ•œ๋‹ค.

 

 

Q. final์„ ์•ˆ์“ฐ๊ฒŒ ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜?

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

 

 

 

 

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

 

 

 

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

 

 

@Autowired

: ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•ด์ฃผ๋Š”๋ฐ, ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์ ์œผ๋กœ ์ฃผ์ž…์ด ๋œ๋‹ค.

 

 

OrderServiceImpl์€ ํด๋ž˜์Šค  MemberRepository์™€ DiscountPolicy์— ์žˆ๋Š” memberRepository์™€ discountPolicy๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ƒ์„ฑ์ž(๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ)๋ฅผ ๋งŒ๋“ค์–ด ๊ทธ ์ƒ์„ฑ์ž๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ํ• ๋‹น๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์ด ์ด๋ค„์ง€๊ฒŒ ๋œ๋‹ค.

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

(์ด๋ ‡๊ฒŒ ์ดํ•ดํ•˜๊ธด ํ–ˆ๋‹ค...)

 

 

 

 

 

 

โ–บ  ์ˆ˜์ •์ž ์ฃผ์ž…

setter๋กœ ํ†ตํ•ด ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…, ์˜์กด๊ด€๊ณ„๋ฅผ ๋ˆ„๋ฝํ•ด๋„ ์‹คํ–‰์€ ๋œ๋‹ค.

 

private MemberRepository memberRepository;
private DiscountPolicy discountPolicy;

    @Autowired
    public void setMemberRepository(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }
    
    @Autowired
    public void setDiscountPolicy(DiscountPolicy discountPolicy) {
        this.discountPolicy = discountPolicy;
    }

 

 

setter๋Š” @Autowired๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ฐ›๊ฒŒ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ฐ ํด๋ž˜์Šค์ธ MemberRepository์™€ DiscountPolicy์—์„œ memberRepository์™€ discountPolicy์— ๋Œ€ํ•œ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๊ฐ์ฒด๋ฅผ final ์—†์ด ์„ ์–ธํ•  ๋•Œ ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ƒ์„ฑ์ž ์ฃผ์ž…์—์„œ๋Š” final์„ ํ†ตํ•ด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋„๋ก ํ–ˆ์ง€๋งŒ setter๋Š” final์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์˜ ์ƒํƒœ์— ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. setter๋Š” ์„ค์ •ํ•˜๋ ค๋Š” ํ•„๋“œ์™€ ๋™์ผํ•œ ํƒ€์ž…์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์•ผํ•˜๋ฉฐ, ์ž๋ฐ”๋นˆ ํ”„๋กœํผํ‹ฐ ๊ทœ์•ฝ์„ ํ†ตํ•ด set---๋ผ๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ทœ์น™์ด ์ƒ๊ฒผ๋‹ค. 

 

 

- setter๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ƒํƒœ ๋ฐ”๊พธ๊ธฐ ์ถœ์ฒ˜

 

[JAVA] ๊ฒŒํ„ฐ(getter)์™€ ์„ธํ„ฐ(setter)

์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ๊ฐ์ฒด ์™ธ๋ถ€์—์„œ ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š”๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์™ธ๋ถ€์—์„œ ๋งˆ์Œ๋Œ€๋กœ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ ๊ฐ์ฒด์˜ ๋ฌด๊ฒฐ์„ฑ(์˜๋„ํ•˜์ง€ ์•Š์€ ์š”์ธ์— ์˜ํ•ด

myblog1128.tistory.com

 

 

 

 

Q. ์ƒ์„ฑ์ž๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋ฐ›์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. setter๋Š” ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ๋ฐ›์•˜๋‹ค. ์™œ ๊ทธ๋Ÿฐ๊ฒƒ์ธ๊ฐ€?

A. ์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•ด ๊ฐ’์„ ์ฃผ์ž…ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ( ๊ฐ’์„ ๋ฐ›์•„์„œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•„๋“œ์— ๋„ฃ์–ด์ค€๋‹ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค.) ๊ทธ๋Ÿฐ๋ฐ setter๋Š” ๊ฐ์ฒด ์ƒํƒœ์— ์ˆ˜์ •์ด ๋“ค์–ด๊ฐ€๋‹ˆ ์ฃผ๋กœ ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. ๊ผญ setter๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๋‚˜์ค‘์— ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด์„œ ํ•˜๋‚˜์”ฉ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ์‹ค์ œ๋กœ setter๋Š” ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๋Š” ์ถ”์„ธ๋ผ๊ณ  ํ•œ๋‹ค. ์ด๊ฑด ์ „์—๋„ ๋‹ค๋ฃฌ์ ์ด ์žˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ, ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ test ์ฝ”๋“œ ํ˜น์€ ํ˜„์žฌ setter ์ฝ”๋“œ ๋“ฑ์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.  

 

 

 

 

 

 

โ–บ  ํ•„๋“œ ์ฃผ์ž…

ํ•„๋“œ์— ๋ฐ”๋กœ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… (์‚ฌ์šฉํ•˜์ง€๋ง์ž)

 

 

    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private DiscountPolicy discountPolicy;

 

OrderServiceImpl์— ๊ฐ ํด๋ž˜์Šค MemberRepository์™€ DiscountPolicy์— ๊ด€ํ•œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์ด ๋ฐ”๋กœ ์ด๋ค„์ง„๋‹ค.

 

 

 

 

 


 

 

 

 

โœ”๏ธ  AutowiredTest

 

@Test
void AutoWiredOption() {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestBean.class);
}

 

 

AnnotationConfigApplicationContext

:  ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, ๋นˆ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

 

 

 

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

 

 

 

static class TestBean {

    @Autowired(required = false)
    public void setNoBean1(Member noBean1) {
        System.out.println("noBean1 = " + noBean1);
    }

    @Autowired
    public void setNoBean2(@Nullable Member noBean2) {
        System.out.println("noBean2 = " + noBean2);
    }

    @Autowired
    public void setBean3(Optional<Member> nobean3) {
        System.out.println("nobean3 = " + nobean3);
    }


}

 

@Autowired(required = false)

: ์ž๋™ ์ฃผ์ž…ํ•  ๋Œ€์ƒ์ด ์—†์œผ๋ฉด ์ˆ˜์ •์ž ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋ฉด ํ˜ธ์ถœ ์ž์ฒด๊ฐ€ ์•ˆ๋œ๋‹ค.

 

 

@Nullable

: ์ž๋™ ์ฃผ์ž…ํ•  ๋Œ€์ƒ์ด ์—†์–ด๋„ ํ˜ธ์ถœ์€ ๋˜์ง€๋งŒ null๊ฐ’์ด ๋œ๋‹ค.

 

 

Optional< >

: ์Šคํ”„๋ง ๋นˆ์ด ์—†์œผ๋ฉด Optional empty๋กœ ๋“ค์–ด์˜จ๋‹ค.

 

 

ํ˜„์žฌ Member ํด๋ž˜์Šค์—๋Š” nobean1, nobean2, nobean3๊ณผ ๊ด€๋ จ๋œ bean์ด ๋“ฑ๋ก๋˜์–ด์žˆ์ง€ ์•Š๊ธฐ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค. nobean1์˜ required = true๋กœ ํ•˜๊ฒŒ๋˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ๋œ๋‹ค. 

 

 

 

Q. AnnotationConfigApplicationContext๋ฅผ ์„ ์–ธ์„ ๋”ฐ๋กœํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ?

A. AnnotationConfigApplicationContext๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ์ดํ•ดํ•ด์•ผํ•œ๋‹ค. AnnotationConfigApplicationContext์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ ํ›„์— bean์„ ๊ด€๋ฆฌํ•˜๊ณ  ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๊ฒŒ ๋œ๋‹ค. static class TestBean์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ๋นˆ์ด ๋œ๋‹ค. ๋งŒ์ผ ๋”ฐ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  static class TestBean์— ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ์ฃผ๊ธฐ๊ฐ€ ๊ผฌ์ผ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค. ์Šคํ”„๋ง์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜์–ด์•ผํ•  ๋นˆ์ด ๊ทธ ์•ˆ์—์„œ ์Šคํ”„๋ง์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ ์–ธํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ์ฃผ๊ธฐ๊ฐ€ ๊ผฌ์—ฌ ์ œ๋Œ€๋กœ ์‹คํ–‰๋˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 


 

 

 

 

Q.  ๋กฌ๋ณต(LomBok) ์ด๋ž€?

A. ์ฝ”๋“œ ๋ฐ˜๋ณต ์ž‘์„ฑ์„ ์ค„์—ฌ์ฃผ๋Š” ์ฝ”๋“œ ๋‹ค์ด์–ดํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

 

 

โœ”๏ธ  OrderServiceImpl (LomBok)

 

 @Component
 @RequiredArgsConstructor
 public class OrderServiceImpl implements OrderService {
     private final MemberRepository memberRepository;
     private final DiscountPolicy discountPolicy;
}

 

 

 @RequiredArgsConstructor

: ์ดˆ๊ธฐํ™” ๋˜์ง€์•Š์€ final ํ•„๋“œ๋‚˜, @NonNull์ด ๋ถ™์€ ํ•„๋“œ์— ๋Œ€ํ•ด ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, @Autowired๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…ํ•œ๋‹ค.

 

 

 

 

- @RequiredArgsConstructor ์ถœ์ฒ˜

 

@RequiredArgsConstructor๋ž€?

Lombok์œผ๋กœ ์Šคํ”„๋ง์—์„œ DI(์˜์กด์„ฑ ์ฃผ์ž…)์˜ ๋ฐฉ๋ฒ• ์ค‘์— ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์ž„์˜์˜ ์ฝ”๋“œ์—†์ด ์ž๋™์œผ๋กœ ์„ค์ •ํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. @RequiredArgsConstructor๋Š” ์ดˆ๊ธฐํ™” ๋˜์ง€์•Š์€ final ํ•„๋“œ๋‚˜, @NonNull ์ด ๋ถ™์€ ํ•„๋“œ

dreamcoding.tistory.com

 

 

 

- ๋กฌ๋ณต ์‚ฌ์šฉ ์˜ˆ์‹œ ์ถœ์ฒ˜

 

[java] ๋กฌ๋ณต lombok ์ด๋ž€?

Lombok ์ด๋ž€ Java ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ฐ˜๋ณต๋˜๋Š” getter, setter, toString .. ๋“ฑ์˜ ๋ฐ˜๋ณต ๋ฉ”์„œ๋“œ ์ž‘์„ฑ ์ฝ”๋“œ๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ์ฝ”๋“œ ๋‹ค์ด์–ดํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Java์—์„œ ๋ณดํ†ต model ํด๋ž˜์Šค๋‚˜ Entity ๊ฐ™์€ ๋„๋ฉ”์ธ ํด๋ž˜์Šค ๋“ฑ์—๋Š” ์ˆ˜๋งŽ

pig-programming.tistory.com

 

 

 


 

 

 

 

โœ”๏ธ  Autowired ํ•„๋“œ ๋ช… ๋งค์น˜

 

<๊ธฐ์กด ์ฝ”๋“œ>

@Autowired
 private DiscountPolicy discountPolicy

 

 

ํ•„๋“œ๊ฐ€ ๋‘ ๊ฐœ (rateDiscountPolicy, fixDiscountPolicy)๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

 

 

<๋ณ€๊ฒฝ ํ›„ ์ฝ”๋“œ>

 @Autowired
 private DiscountPolicy rateDiscountPolicy

 

 

@Autowired๋Š” DiscountPolicy๋ฅผ ํƒ€์ž…์˜ ๋นˆ์„ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•ด DiscountPolicy๋ฅผ ์ฐพ์•„๋‚˜์„œ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ DiscountPolicy ํƒ€์ž…์—๋Š” rateDiscounPolicy์™€ fixDiscountPolicy์ธ ๋™์ผ ํƒ€์ž…์˜ ๋นˆ์ด ๋‘ ๊ฐœ๊ฐ€ ์žˆ์Œ์„ ์•Œ๊ฒŒ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ํƒ€์ž… ๊ธฐ๋ฐ˜์—์„œ ์ด๋ฆ„ ๊ธฐ๋ฐ˜ ์ฝ”๋“œ๋กœ ์ˆ˜์ •์‹œ์ผœ์ค€ ๊ฒƒ์ด๋‹ค. rateDiscountPolicy ๋นˆ์ด ์ œ๋Œ€๋กœ ๋“ฑ๋ก๋˜์–ด์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋นˆ์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.

 

 

 

 


 

 

 

โœ”๏ธ  @Qualifier ์‚ฌ์šฉ

 

 

โ–บ  ๋นˆ ๋“ฑ๋ก์‹œ @Qualifier  ์ถ”๊ฐ€ ๊ตฌ๋ถ„์ž ์ ์–ด์ฃผ๊ธฐ

 

 @Component
 @Qualifier("mainDiscountPolicy")
public class RateDiscountPolicy implements DiscountPolicy {}

 

 

Q. ๊ฐ™์€ ํƒ€์ž…์˜ ๋‹ค๋ฅธ ๋นˆ์—๋„ @Qualifier์„ ๊ผญ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”๊ฐ€?

A. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ด์„œ ๊ผญ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ์„œ ๋ฌด์กฐ๊ฑด ๋ถ™์—ฌ์•ผํ•˜๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ๊ฐ™์€ ํƒ€์ž…์˜ ๋นˆ์ด 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ ๊ตฌ๋ถ„์ž๋ฅผ ๋ถ™์ด์ง€ ์•Š์€ ๋‹ค๋ฅธ ๋นˆ์„ ๊ฐ€์ ธ์˜ค๊ณ ์ž ํ•  ๋•Œ  ๊ตฌ๋ถ„์ž๋ฅผ ๋ถ™์ด์ง€ ์•Š์€ ๋˜ ๋‹ค๋ฅธ ๋นˆ๊ณผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋‹ค๋ฅธ ๋นˆ์—๋„ @Qualifier์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„์ง€์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์•„๋ณด์ธ๋‹ค. 

 

 

 

 

โ–บ  ์ƒ์„ฑ์ž ์ž๋™ ์ฃผ์ž…

public OrderServiceImpl(MemberRepository memberRepository
		,@Qualifier("mainDiscountPolicy") DiscountPolicy discountPolicy) {

 

์—ฌ๊ธฐ์„œ @Qulifier์„ ๋ถ™์—ฌ์ฃผ๋ฉด ์„œ๋กœ ๊ฐ™์€ @Qulifier์„ ์ฐพ์œผ๋Ÿฌ ๋‹ค๋‹Œ๋‹ค. ๋งŒ์ผ ๋ถ™์ด์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•ด๋„ mainDiscountPolicy์ธ ์Šคํ”„๋ง ๋นˆ์„ ์ฐพ์œผ๋Ÿฌ ๋‹ค๋‹ ๊ฒƒ์ด๋‹ค. 

 

 

 

 


 

 

 

โœ”๏ธ  @Primary ์‚ฌ์šฉ

 @Component
 @Primary
 public class RateDiscountPolicy implements DiscountPolicy {}

 

@Primary๋ฅผ ๋ถ™์—ฌ์„œ DiscountPolicy์˜ ํƒ€์ž…์—์„œ RateDiscountPolicy ๋นˆ์ด ๊ฐ€์žฅ ์šฐ์„ ์ ์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…๋˜๋„๋ก ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋ฉ”์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ปค๋„ฅ์…˜ ํš๋“์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ @Primary๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์„œ๋ธŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜ ํš๋“์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ @Qualify๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์šฉ์ดํ•˜๋‹ค.

 

 

 

 


 

 

 

โœ”๏ธ  MainDiscountPolicy

์• ๋…ธํ…Œ์ด์…˜ ์ง์ ‘ ๋งŒ๋“ค๊ธฐ

 

 

 @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
 ElementType.TYPE, ElementType.ANNOTATION_TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Qualifier("mainDiscountPolicy")

 

@Target

: ์• ๋…ธํ…Œ์ด์…˜ ์ ์šฉ ๋Œ€์ƒ

 

 

@Retention

: ์• ๋…ธํ…Œ์ด์…˜ ์ง€์† ์‹œ๊ฐ„ ๊ฒฐ์ •

 

 

@Documented

: ์• ๋…ธํ…Œ์ด์…˜ ์ •๋ณด๋ฅผ javadoc์œผ๋กœ ์ž‘์„ฑํ•œ ๋ฌธ์„œ์— ํฌํ•จ

 

 

@Qualifier("mainDiscountPolicy")` ์ด๋ ‡๊ฒŒ ๋ฌธ์ž๋ฅผ ์ ์œผ๋ฉด ์ปดํŒŒ์ผ์‹œ ํƒ€์ž… ์ฒดํฌ๊ฐ€ ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์• ๋…ธํ…Œ์ด์…˜์„ ์ง์ ‘ ๋งŒ๋“ค์ฃผ์—ˆ๋‹ค. 

 

 

 

- ์• ๋…ธํ…Œ์ด์…˜ ์ถœ์ฒ˜

 

[Java] ์ž๋ฐ” ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ฆฌ ๋ฐ ํ™œ์šฉ

๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜ (Meta Annotation) ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์€ ์–ด๋…ธํ…Œ์ด์…˜์— ๋ถ™์ด๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. ์ฆ‰, ์‚ฌ์šฉํ•  ์–ด๋…ธํ…Œ์ด์…˜์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์€ ์‚ฌ์šฉํ•  ์–ด๋…ธํ…Œ์ด์…˜์˜ ์ ์šฉ๋Œ€์ƒ ๋˜

ittrue.tistory.com

 

 

 

@MainDiscountPolicy
public class RateDiscountPolicy implements DiscountPolicy { } 

 

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ์ƒ์„ฑ์ž ์ฃผ์ž…์ด๋‚˜ ์ˆ˜์ •์ž ์ฃผ์ž…์—๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 

 

 


 

 

 

โœ”๏ธ  AllBeanTest

ํ•ด๋‹น ํƒ€์ž…์˜ ๋ชจ๋“  ๋นˆ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ Map๊ณผ List๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

โ–บ  findAllBean( )

 
   ApplicationContext ac
     = new AnnotationConfigApplicationContext(AutoAppConfig.class, DiscountService.class);

	DiscountService discountService = ac.getBean(DiscountService.class);
        Member member = new Member(1L, "userA", Grade.VIP);
        int discountPrice = discountService.discount(member, 10000, "fixDiscountPolicy");

 

AutoAppConfig์™€ DiscountService ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ac๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  DiscountService ๊ฐ์ฒด๋ฅผ ์ฐพ์•„  DiscountService ํƒ€์ž…์˜ discountService ์ด๋ฆ„์œผ๋กœ ํ•ด๋‹น ๋นˆ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. member๋Š” 10000์„ ์ฃผ๋ฌธํ–ˆ๊ณ , Vip ๋“ฑ๊ธ‰์œผ๋กœ fixDiscountPolicy๋ฅผ ์จ์„œ discount๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

 

 

 

- getBean ์ถœ์ฒ˜

 

[Spring] getBean() ๋ฉ”์„œ๋“œ์˜ ์—ญํ• ๊ณผ ํ™œ์šฉ ๋ฐฉ๋ฒ•

Spring ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ApplicationContext๋ฅผ ํ†ตํ•ด ๋นˆ(Bean) ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. ApplicationContext๋Š” getBean() ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ๋“ฑ๋ก๋œ ๋นˆ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” getBean(

jjangadadcodingdiary.tistory.com

 

 

 

 

โ–บ  DiscountService

public DiscountService(Map<String, DiscountPolicy> policyMap,List<DiscountPolicy> policies){
            this.policyMap = policyMap;
            this.policies = policies;
}

 

์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ƒ์„ฑ์ž๋Š” DiscountPolicy ํƒ€์ž…์„ <๋นˆ์ด๋ฆ„, ๋นˆ> ์Œ์œผ๋กœ ๋ฐ›๋Š” Map์ด ์žˆ๊ณ , ๋ชจ๋“  DiscountPolicy ๋นˆ์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฐ›๋Š” List๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. 

 

 

 

 

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

 

 

 

 public int discount(Member member, int price, String discountCode) {
            DiscountPolicy discountPolicy = policyMap.get(discountCode); 
            return discountPolicy.discount(member, price);
}

 

์ด ๋ฉ”์„œ๋“œ๊ฐ€ ํ• ์ธ ์ •์ฑ…์„ ์ ์šฉํ•˜๊ฒŒ ํ•œ๋‹ค. discountCode๋Š” Test ์ฝ”๋“œ์—์„œ fixDiscountPolicy๋กœ ๋ฐ›์•˜๋‹ค. ๊ทธ๋Ÿฌ๋ฉด policyMap์„ ํ†ตํ•ด fixDiscountPolicy ๋นˆ์„ ์ฐพ์•„ discountPolicy๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐ›๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฉด discountPolicy๋Š” fixDiscountPolicy๊ฐ€ ๋˜๋Š”๊ฑฐ๊ณ  fixDiscountPolicy์˜ ์ •์ฑ…์ธ 'Vip๋Š” ์–ผ๋งˆ๋ฅผ ๊ตฌ๋งคํ•˜๋“  1000์›์„ ํ• ์ธํ•ด์ฃผ๋Š” ๊ฒƒ'์— ๋งž์ถฐ ํ• ์ธ์ด ์ง„ํ–‰๋œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด member๋Š” vip์ด๋ฉฐ 10000์›์„ ๊ตฌ๋งคํ–ˆ๋‹ค๋Š” test ์ฝ”๋“œ์— ๋งž์ถฐ 1000์›์„ ํ• ์ธํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

Map๊ณผ List๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒ€์ž…์— ๋งž๋Š” ๋ชจ๋“  ๋นˆ์„ ๊ฐ€์ ธ์˜จ ํ›„, ๊ณ ๊ฐ ํ˜น์€ ํ…Œ์ŠคํŠธ์— ๋งž๋Š” ๋นˆ์„ ์ฐพ์•„ ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

 

 

Q. ๋ชจ๋“  ๋นˆ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๊ผญ Map๊ณผ List ๋‘˜ ๋‹ค ์จ์•ผํ•˜๋Š”๊ฑธ๊นŒ?

A. ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์„œ ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. Map์€ <ํ‚ค ํ˜น์€ ์ด๋ฆ„>์„ ๊ฐ€์ง€๊ณ  ๋นˆ์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๋Š” ๊ฒƒ์ด๊ณ , List๋Š” ์ˆœ์„œ์— ๋”ฐ๋ผ ๋ชจ๋“  ๋นˆ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ํ˜„์žฌ๋Š” ์ ์€ ๋นˆ์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์ง€๋งŒ ์‹ค๋ฌด๋Š” ํ›จ์”ฌ ๋งŽ์€ ๋นˆ์œผ๋กœ ํ…Œ์ŠคํŠธ ํ˜น์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋Œ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ํŠน์„ฑ์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ์œ ์—ฐํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

+ Recent posts