# Spring / Spring Container, Bean
Study Repository

Spring / Spring Container, Bean

by rlaehddnd0422

 ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

 

ApplicationContext๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ธํ„ฐํŽ˜์ด์Šค

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” XML์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์ž๋ฐ” ์„ค์ • ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

>  AppConfig๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ์‹์ด ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์ž๋ฐ” ์„ค์ • ํด๋ž˜์Šค๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ ๋ฐฉ์‹

 

AnnotationConfigApplicationContext๋Š” ApplicationContext Inferface์˜ ๊ตฌํ˜„์ฒด

 

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

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ๊ณผ์ •

1. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•จ.

์—ฌ๊ธฐ์„œ๋Š” AppConfig.class๋ฅผ ๊ตฌ์„ฑ์ •๋ณด๋กœ ์ง€์ •ํ–ˆ์Œ

 

2. ์Šคํ”„๋ง ๋นˆ ๋“ฑ๋ก

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜์–ด์˜จ ์„ค์ • ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•œ๋‹ค.

@Bean ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋กํ•จ

โ—๏ธ๊ทธ๋ฆผ์—๋Š” ํ‘œ์‹œํ•˜์ง€ ์•Š์•˜์ง€๋งŒ AppConfig๋„ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

 

๋นˆ ์ด๋ฆ„์€ @Bean(name="...")์„ ํ†ตํ•ด ์ง์ ‘ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค.

โ—๏ธ๋นˆ ์ด๋ฆ„์€ ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋œ๋‹ค. ๋‹ค๋ฅธ ๋นˆ์ด ๋ฌด์‹œ๋˜๊ฑฐ๋‚˜ ์„ค์ •์— ๋”ฐ๋ผ ๊ธฐ์กด ๋นˆ์„ ๋ฎ์–ด๋ฒ„๋ฆฌ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•จ

 

 

3. ์˜์กด๊ด€๊ณ„ ์„ค์ • 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์„ค์ • ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ DI๋ฅผ ์ˆ˜ํ–‰ํ•จ.

๋‹จ์ˆœ ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ง€๋งŒ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. ์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ์—์„œ ์„ค๋ช…

 

โ—๏ธ ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ , DI๋ฅผ ํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ๋‚˜๋ˆ„์–ด์ ธ์žˆ๋‹ค. ์ž๋ฐ”์ฝ”๋“œ๋กœ ์Šคํ”„๋ง๋นˆ์„ ๋“ฑ๋กํ•˜๋ฉด ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ DI๋„ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ๋œ๋‹ค.


์Šคํ”„๋ง ๋นˆ ์กฐํšŒ

 ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์กฐํšŒ

public class ApplicationContextInfoTest {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test
    @DisplayName("๋ชจ๋“  ๋นˆ ์ถœ๋ ฅ")
    void findAllBean()
    {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) 
        {
            Object bean = ac.getBean(beanDefinitionName);
            System.out.println("name = " + beanDefinitionName + " object = " + bean);

        }
    }
    @Test
    @DisplayName("์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นˆ ์ถœ๋ ฅ")
    void findApplicationBean()
    {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames)
        {
            BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);

            // Role Role_APPLICATION : ์ง์ ‘ ๋“ฑ๋กํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นˆ
            // Role Role_INFASTRUCTURE : ์Šคํ”„๋ง ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๋นˆ
            if(beanDefinition.getRole()==BeanDefinition.ROLE_APPLICATION)
            {
                Object bean = ac.getBean(beanDefinitionName);
                System.out.println("name = " + beanDefinitionName + " object = " + bean);
            }
        }
    }
}

โ–ถ๏ธŽ ๋ชจ๋“  ๋นˆ ์ถœ๋ ฅ 

ac.getBeanDefinitionNames() : ์Šคํ”„๋ง์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์ด๋ฆ„์„ ์กฐํšŒ

ac.getBean() : Bean ์ด๋ฆ„์œผ๋กœ Bean ๊ฐ์ฒด ์กฐํšŒ 

 

โ–ถ๏ธŽ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นˆ ์ถœ๋ ฅ

์Šคํ”„๋ง์ด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋นˆ์€ ์ œ์™ธํ•˜๊ณ , ๋‚ด๊ฐ€ ๋“ฑ๋กํ•œ ๋นˆ๋งŒ ์กฐํšŒ

getRole()๋กœ ๊ตฌ๋ถ„ ๊ฐ€๋Šฅ 

- getRole() == ROLE_APPLICATION : ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ Bean

- getRole() == ROLE_INFRASTRUCTURE : ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ Bean

 

์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ๊ธฐ๋ณธ

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์Šคํ”„๋ง ๋นˆ์„ ์ฐพ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์กฐํšŒ ๋ฐฉ๋ฒ•

ac.getBean(name, type)

ac.getBean(type)

 

์กฐํšŒ ๋Œ€์ƒ ์Šคํ”„๋ง ๋นˆ์ด ์—†์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ

- NoSuchBeanDefinitionException : No bean named 'xxxxxx' available

 

package hello.core.beanfind;

import hello.core.AppConfig;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImp;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.jupiter.api.Assertions.*;

public class ApplicationContextBasicFindTest {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test
    @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ")
    void findBeanByName()
    {
        MemberService memberService = ac.getBean("memberService", MemberService.class);
        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImp.class);
    }

    @Test
    @DisplayName("๊ตฌ์ฒด ํƒ€์ž…์œผ๋กœ ์กฐํšŒ")
    void findBeanByName2()
    {
        MemberService memberService = ac.getBean("memberService", MemberServiceImp.class);
        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImp.class);
    }

    @Test
    @DisplayName("ํƒ€์ž…์œผ๋กœ๋งŒ ์กฐํšŒ")
    void findBeanByType()
    {
        MemberService memberService = ac.getBean(MemberService.class);
        Assertions.assertThat(memberService).isInstanceOf(MemberServiceImp.class);
    }


    @Test
    @DisplayName("๋นˆ ์ด๋ฆ„์œผ๋กœ ์กฐํšŒ (์‹คํŒจ)")
    void findBeanByNameX()
    {
        // MemberService xxxxxx = ac.getBean("xxxxxx", MemberService.class);

        // ์˜ค๋ฅธ์ชฝ์—์žˆ๋Š” ๋กœ์ง์‹คํ–‰์‹œ ์™ผ์ชฝException ๋ฐœ์ƒํ•ด์•ผํ•จ
        assertThrows(NoSuchBeanDefinitionException.class, ()->
                ac.getBean("xxxxxx", MemberService.class));

    }
}

โ—๏ธ ๊ตฌ์ฒดํƒ€์ž…์œผ๋กœ ์กฐํšŒ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

 

์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ๋™์ผํ•œ ํƒ€์ž… ๋‘˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ

package hello.core.beanfind;

import hello.core.AppConfig;
import hello.core.MemberRepository;
import hello.core.MemoryMemberRepository;
import hello.core.discount.DiscountPolicy;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ApplicaitonContextSamebeanFindTest {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class);

    @Test
    @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ์ค‘๋ณต์˜ค๋ฅ˜ ๋ฐœ์ƒ")
    void findBeanByTypeDuplicate()
    {
//        MemberRepository bean = ac.getBean(MemberRepository.class);
        assertThrows(NoUniqueBeanDefinitionException.class,()->ac.getBean(MemberRepository.class));
    }

    @Test
    @DisplayName("ํƒ€์ž…์œผ๋กœ ์กฐํšŒ์‹œ ๊ฐ™์€ ํƒ€์ž…์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด, ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค")
    void findBeanByName()
    {
        MemberRepository memberRepository1 = ac.getBean("memberRepository1", MemberRepository.class);
        assertThat(memberRepository1).isInstanceOf(MemberRepository.class);
    }

    @Test
    @DisplayName("ํŠน์ • ํƒ€์ž… ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
    void findBeanByType() {
        Map<String, MemberRepository> beansOfType = ac.getBeansOfType(MemberRepository.class);
        for(String key : beansOfType.keySet())
        {
            System.out.println("key = " + key + " value = " + beansOfType.get((key)));
        }
        System.out.println("beansOfType = " + beansOfType);
        assertThat(beansOfType.size()).isEqualTo(2);

    }


    @Configuration
    static class SameBeanConfig
    {
        @Bean
        public MemberRepository memberRepository1()
        {
            return new MemoryMemberRepository();
        }

        @Bean
        public MemberRepository memberRepository2()
        {
            return new MemoryMemberRepository();
        }

    }
}

 

ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ ๊ฐ™์€ ํƒ€์ž…์˜ ์Šคํ”„๋ง ๋นˆ์ด ๋‘˜ ์ด์ƒ ์žˆ์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ.

โ–ถ๏ธŽ ์ด๋•Œ๋Š” ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ž.

 

ac.getBeansOfType()์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํƒ€์ž…์˜ ๋ชจ๋“  ๋นˆ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์Šคํ”„๋ง ๋นˆ ์กฐํšŒ - ์ƒ์†๊ด€๊ณ„

๋ถ€๋ชจํƒ€์ž…์„ ์กฐํšŒํ•˜๋ฉด ์ž์‹ ํƒ€์ž…๋„ ํ•จ๊ป˜ ์กฐํšŒํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ๋ชจ๋“  ์ž๋ฐ” ๊ฐ์ฒด์˜ ์ตœ๊ณ  ๋ถ€๋ชจ์ธ 'Object' ํƒ€์ž…์„ ์กฐํšŒํ•˜๋ฉด ๋ชจ๋“  ์Šคํ”„๋ง ๋นˆ์„ ์กฐํšŒํ•œ๋‹ค.

 

package hello.core.beanfind;

import hello.core.AppConfig;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ApplicationContextExtendsFindTest {
    @Configuration
    static class TestConfig {
        @Bean
        public DiscountPolicy rateDiscountPolicy() {
            return new RateDiscountPolicy();
        }

        @Bean
        public DiscountPolicy fixDiscountPolicy() {
            return new FixDiscountPolicy();
        }
    }

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ ์ž์‹ ๋‘˜ ์ด์ƒ์žˆ์œผ๋ฉด ์ค‘๋ณต ์˜ค๋ฅ˜ ๋ฐœ์ƒ")
    void findBeanByParentTypeDuplicate() {
        assertThrows(NoUniqueBeanDefinitionException.class, () -> ac.getBean(DiscountPolicy.class));
    }

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ์‹œ ์ž์‹ ๋‘˜ ์ด์ƒ์žˆ์œผ๋ฉด ๋นˆ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค")
    void findBeanByParentTypeBeanName() {
        DiscountPolicy rateDiscountPolicy = ac.getBean("rateDiscountPolicy", DiscountPolicy.class);
        assertThat(rateDiscountPolicy).isInstanceOf(RateDiscountPolicy.class);
    }

    @Test
    @DisplayName("ํŠน์ • ํ•˜์œ„ ํƒ€์ž…์œผ๋กœ ์กฐํšŒ")
    void findBeanBySubType() {
        RateDiscountPolicy bean = ac.getBean(RateDiscountPolicy.class);
        assertThat(bean).isInstanceOf(RateDiscountPolicy.class);
    }

    @Test
    @DisplayName("๋ถ€๋ชจ ํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
    void findBeanByParentAll() {
        Map<String, DiscountPolicy> beansOfType = ac.getBeansOfType(DiscountPolicy.class);
        assertThat(beansOfType.size()).isEqualTo(2);
        for (String key : beansOfType.keySet()) {
            System.out.println("key = " + key + "value = " + beansOfType.get(key));
        }
    }

    @Test
    @DisplayName("Objectํƒ€์ž…์œผ๋กœ ๋ชจ๋‘ ์กฐํšŒํ•˜๊ธฐ")
    void findAllBeansByObj()
    {
        Map<String, Object> beansOfType = ac.getBeansOfType(Object.class);
        for (String key : beansOfType.keySet()) {
            System.out.println("key = " + key + "value = " + beansOfType.get(key));
        }
    }
}

BeanFactory์™€ ApplicationContext

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์†๊ด€๊ณ„



 

 

 

 

 

 

 

 

 

 

BeanFactory

  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ตœ์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค
  • ์Šคํ”„๋ง ๋นˆ์„ ๊ด€๋ฆฌ, ์กฐํšŒํ•˜๋Š” ์—ญํ•  ๋‹ด๋‹น
  • getBean() ์ œ๊ณต
  • ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ๋Šฅ์ด ์„ ์–ธ๋˜์–ด ์žˆ์Œ

 

ApplicationContext 

  • BeanFactory๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ƒ์†๋ฐ›์•„ ์ œ๊ณต ( ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€๋Š” ์•Š์Œ )
  • ๋นˆ์„ ๊ด€๋ฆฌ,๊ฒ€์ƒ‰ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์™ธ์—๋„ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋‹ค.

ApplicationContext ๋‹ค์ด์–ด๊ทธ๋žจ

 

1. ๋ฉ”์‹œ์ง€์†Œ์Šค๋ฅผ ํ™œ์šฉํ•œ ๊ตญ์ œํ™” ๊ธฐ๋Šฅ

์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํ•œ๊ตญ์—์„œ ๋“ค์–ด์˜ค๋ฉด ํ•œ๊ตญ์–ด๋กœ, ์˜์–ด๊ถŒ์—์„œ ๋“ค์–ด์˜ค๋ฉด ์˜์–ด๋กœ ์ถœ๋ ฅ

 

2. ํ™˜๊ฒฝ๋ณ€์ˆ˜

๋กœ์ปฌ, ๊ฐœ๋ฐœ, ์šด์˜๋“ฑ์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ฒ˜๋ฆฌ

 

3. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฒคํŠธ

์ด๋ฒคํŠธ๋ฅผ ๋ฐœํ–‰ํ•˜๊ณ  ๊ตฌ๋…ํ•˜๋Š” ๋ชจ๋ธ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ง€์›

 

4. ํŽธ๋ฆฌํ•œ ๋ฆฌ์†Œ์Šค ์กฐํšŒ

ํŒŒ์ผ, ํด๋ž˜์ŠคํŒจ์Šค, ์™ธ๋ถ€ ๋“ฑ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒ

 

 

<์ •๋ฆฌ>

  • ApplicationContext๋Š” BeanFactory์˜ ๊ธฐ๋Šฅ์„ ์ƒ์†๋ฐ›๋Š”๋‹ค.
  • ApplicationContext๋Š” ๋นˆ ๊ด€๋ฆฌ๊ธฐ๋Šฅ + ํŽธ๋ฆฌํ•œ ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ์„ ์ œ๊ณต.
  • BeanFactory๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค. ๋ถ€๊ฐ€๊ธฐ๋Šฅ์ด ํฌํ•จ๋œ ApplicationContext๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • BeanFactory๋‚˜ ApplicationContext๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ ํ•œ๋‹ค.

๋‹ค์–‘ํ•œ Config ํ˜•์‹ ์ง€์› - java, XML

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ๋‹ค์–‘ํ•œ ํ˜•์‹์˜ config ์ •๋ณด๋ฅผ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๊ฒŒ ์„ค๊ณ„๋˜์—ˆ๋‹ค.

 

appConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="memberRepository" class = "hello.core.MemoryMemberRepository">
    </bean>

    <bean id="discountPolicy" class = "hello.core.discount.RateDiscountPolicy">
    </bean>

    <bean id="memberService" class = "hello.core.member.MemberServiceImp">
        <constructor-arg name="memberRepository" ref="memberRepository"/>
    </bean>

    <bean id="orderService" class = "hello.core.order.OrderServiceImp">
        <constructor-arg index="0" name="memberRepository" ref="memberRepository"/>
        <constructor-arg index="1" name="discountPolicy" ref="discountPolicy"/>
    </bean>

</beans>

 

TestCode

public class XmlAppContext {

    @Test
    void xmlAppContext()
    {
        ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");
        MemberService memberService = ac.getBean("memberService",MemberService.class);
        assertThat(memberService).isInstanceOf(MemberService.class);
    }
}

 


์Šคํ”„๋ง ๋นˆ ์„ค์ • ๋ฉ”ํƒ€ ์ •๋ณด - BeanDefinition

์Šคํ”„๋ง์€ ๋‹ค์–‘ํ•œ Config ํ˜•์‹์„ BeanDefinition์ด๋ผ๋Š” ์ถ”์ƒํ™”๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์›ํ•œ๋‹ค.

 

BeanDefinition ์ •๋ณด

BeanClassName: ์ƒ์„ฑํ•  ๋นˆ์˜ ํด๋ž˜์Šค ๋ช…(์ž๋ฐ” ์„ค์ • ์ฒ˜๋Ÿผ ํŒฉํ† ๋ฆฌ ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—†์Œ)

factoryBeanName: ํŒฉํ† ๋ฆฌ ์—ญํ• ์˜ ๋นˆ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ด๋ฆ„, ์˜ˆ) appConfig

factoryMethodName: ๋นˆ์„ ์ƒ์„ฑํ•  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ์ง€์ •, ์˜ˆ) memberService
Scope:
์‹ฑ๊ธ€ํ†ค(๊ธฐ๋ณธ๊ฐ’)

 

package hello.core.beanDefinition;

import hello.core.AppConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanDefinitionTest {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

    @Test
    @DisplayName("๋นˆ ์„ค์ • ๋ฉ”ํƒ€์ •๋ณด ํ™•์ธ")
    void findApplicationBean()
    {
        String [] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);

            if(beanDefinition.getRole()==BeanDefinition.ROLE_APPLICATION)
            {
                System.out.println("beanDefinitionName = " + beanDefinitionName + " beanDefinition = " + beanDefinition);
            }
        }
    }
}

<์ •๋ฆฌ>

BeanDefinition์„ ์ง์ ‘ ์ƒ์„ฑํ•ด์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹ค๋ฌด์—์„œ BeanDefinition์„ ์ง์ ‘ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•  ์ผ์€ ์—†๋‹ค.

BeanDefinition์„ ํ†ตํ•ด ์Šคํ”„๋ง์ด ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ์„ค์ • ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ •๋„๋งŒ ์•Œ๊ณ  ์žˆ์ž.

๊ฐ€๋” ์Šคํ”„๋ง ์ฝ”๋“œ๋‚˜ ์Šคํ”„๋ง ๊ด€๋ จ ์˜คํ”ˆ ์†Œ์Šค์˜ ์ฝ”๋“œ๋ฅผ ๋ณผ ๋•Œ, BeanDefinition ์ด๋ผ๋Š” ๊ฒƒ์ด ๋ณด์ผ ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ๋•Œ ์ด๋Ÿฌํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋– ์˜ฌ๋ฆฌ๋ฉด ๋œ๋‹ค.

 

 


<์ฐธ๊ณ ์ž๋ฃŒ>

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

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ