๐Ÿง‘๐Ÿปโ€๐Ÿ’ป@Configuration๊ณผ ์‹ฑ๊ธ€ํ†ค

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

์•ž์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด์•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ AppConfig๋ฅผ ๋ณด๋ฉด ์ด์ƒํ•œ ์ ์ด ์žˆ๋‹ค...๐Ÿค”

@Configuration
public class AppConfig {

    @Bean //spring container ์— ๋“ฑ๋ก๋จ
    public MemberService memberService(){ 
        return new MemberServiceImpl(memberRepository()); // DI ์ฃผ์ž… (์˜์กด๊ด€๊ณ„ ์ฃผ์ž…)
    }

    @Bean
    public OrderService orderService(){
        return new OrderServiceImpl(memberRepository(), discountPolicy());
    }
    
    @Bean
    public MemberRepository memberRepository(){
    	return new MemoryMemberRepository();
    }
    ...
}

์œ„ ์ฝ”๋“œ์—์„œ memberService ๋นˆ์„ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด memberRepository()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ‘์— new MemoryMemberRepostiory()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , orderService๋นˆ์„ ๋งŒ๋“œ๋Š” ๋ถ€๋ถ„์—์„œ๋„ memberRepository()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ‘์— new MemoryMemberRepostiory()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

์ฆ‰ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋‘๊ฐœ์˜ MemoryMemberRepository๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ ๊ฐ™๊ณ  ์ด๋Š” ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์ด ๊นจ์ง€๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค.


๊ณผ์—ฐ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์ธ์ง€ ์•Œ์•„๋ณด์ž!!

public class MemberServiceImpl implements MemberService{

    private final MemberRepository memberRepository;

    //ํ…Œ์ŠคํŠธ ์šฉ๋„
    public MemberRepository getMemberRepository(){
        return memberRepository;
    }
}



public class OrderServiceImpl implements OrderService{
	
    private final MemberRepository memberRepository;

 //ํ…Œ์ŠคํŠธ ์šฉ๋„
    public MemberRepository getMemberRepository() {
        return memberRepository;
    }
}




public class ConfigurationSingletonTest {

    @Test
    void configurationTest(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

        MemberServiceImpl memberService = ac.getBean("memberService", MemberServiceImpl.class);
        OrderServiceImpl orderService = ac.getBean("orderService", OrderServiceImpl.class);
        MemberRepository memberRepository = ac.getBean("memberRepository", MemberRepository.class);

        MemberRepository memberRepository1 = memberService.getMemberRepository();
        MemberRepository memberRepository2 = orderService.getMemberRepository();

        //๋ชจ๋‘ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋‹ค.
        System.out.println("memberService -> memberRepository = " + memberRepository1);
        System.out.println("orderService -> memberRepository = " + memberRepository2);
        System.out.println("memberRepository = " + memberRepository);

        assertThat(memberService.getMemberRepository()).isSameAs(memberRepository);
        assertThat(orderService.getMemberRepository()).isSameAs(memberRepository);

    }

์œ„์—์„œ ์šฐ๋ คํ•œ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ณต์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค...๐Ÿ˜ฒ
ํ˜ธ์ถœ์ด ๋ช‡๋ฒˆ ๋˜๋Š”์ง€ ์ข€ ๋” ๋””ํ…Œ์ผ ํ•˜๊ฒŒ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜๋‹ค.

@Configuration
public class AppConfig {

    @Bean //spring container ์— ๋“ฑ๋ก๋จ
    public MemberService memberService(){ //๋ฆฌํŒฉํ† ๋ง์„ ํ†ตํ•ด ์—ญํ• ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณด์—ฌ์คŒ, ์ค‘๋ณต์ œ๊ฑฐ
        System.out.println("call AppConfig.memberService");
        return new MemberServiceImpl(memberRepository()); // DI ์ฃผ์ž… (์˜์กด๊ด€๊ณ„ ์ฃผ์ž…)
    }


    @Bean
    public MemberRepository memberRepository() {
        System.out.println("call AppConfig.memberRepository");
        return new MemoryMemberRepository();
    }

    @Bean
    public OrderService orderService(){
        System.out.println("call AppConfig.orderService");
        return new OrderServiceImpl(memberRepository(), discountPolicy());
    }

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

์›๋ž˜๋Œ€๋กœ๋ผ๋ฉด memberRepository()๋Š” 3๋ฒˆ ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ @Configuration ๋•๋ถ„์ด๋‹ค.
์œ„์— ์ฝ”๋“œ์—์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ์ด AppConfig ํด๋ž˜์Šค ์ƒ๋‹จ์—๋Š” @Configuration์ด ๋ถ™์–ด ์žˆ๋‹ค.


public class ConfigurationSingletonTest {

    @Test
    void configurationDeep(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
        AppConfig bean = ac.getBean(AppConfig.class);

        System.out.println("bean = " + bean.getClass()); //bean = class hello.core.AppConfig$$EnhancerBySpringCGLIB$$5b1c10ee

    }
}

์—ฌ๊ธฐ์„œ AnnotationConfigApplicationContext()์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธด ๊ฐ’์€ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค. ๊ทธ๋ž˜์„œ AppConfig๋„ ์Šคํ”„๋ง ๋นˆ์ธ ๊ฒƒ์ด๋‹ค!

AppConfig ํด๋ž˜์Šค๋Š” ์ˆœ์ˆ˜ํ•œ ํด๋ž˜์Šค๋ผ๋ฉด class hello.core.AppConfig๋กœ ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋’ค์— ์ด์ƒํ•œ ๊ฒƒ๋“ค์ด ๋ถ™์–ด์žˆ๋‹ค....๐Ÿง


์ด๋Š” ์Šคํ”„๋ง์ด CGLIB์ด๋ผ๋Š” ๋ฐ”์ดํŠธ์ฝ”๋“œ ์กฐ์ž‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ AppConfig ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž„์˜์˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ . ๊ทธ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ ๊ฒƒ์ด๋‹ค.

๊ทธ ๋‹ค๋ฅธ ํด๋ž˜์Šค๊ฐ€ ์‹ฑ๊ธ€ํ†ค์„ ๋ณด์žฅํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.
๊ทธ๋ž˜์„œ ์œ„์—์„œ์™€ ๊ฐ™์ด memberRepositroy()๊ฐ€ 3๋ฒˆ์ด ์•„๋‹Œ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ ๊ฒƒ์ด๋‹ค.

๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ AppConfigํด๋ž˜์Šค์—์„œ @Configuration์„ ์—†์•ค๋‹ค๋ฉด memberRepository()๋Š” 3๋ฒˆ ํ˜ธ์ถœ๋˜๊ณ  ์‹ฑ๊ธ€ํ†ค์€ ๊นจ์ ธ ๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค...๐Ÿ˜ญ

์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋ฉด @Bean๋งŒ ์‚ฌ์šฉํ•ด๋„ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก์€ ๋˜์ง€๋งŒ, ์‹ฑ๊ธ€ํ†ค์€ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • ์œ„์—์„œ memberRepository()์ฒ˜๋Ÿผ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์ด ํ•„์š”ํ•ด์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•  ๋•Œ ์‹ฑ๊ธ€ํ†ค์€ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.


๐Ÿ“ ๋”ฐ๋ผ์„œ ์Šคํ”„๋ง ์„ค์ • ์ •๋ณด๋Š” ํ•ญ์ƒ @Configuration์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹คโ—๏ธ



์ถœ์ฒ˜

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๊ฐœ์˜ ๋Œ“๊ธ€