Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[빈 후처리기] 예제코드 1 - 일반적인 스프링 빈 등록 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

[빈 후처리기] 예제코드 1 - 일반적인 스프링 빈 등록

Seung__ 2022. 1. 2. 22:32

1. 일반적인 스프링 빈 등록


public class BasicTest {

    @Test
    void basicConfig (){

        //스프링 컨테이너
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BasicConfig.class);

        //A는 빈으로 등록됨.
        A a = applicationContext.getBean("beanA", A.class);
        a.helloA();

        //B는 스프링 빈으로 등록되지 않음.
        Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> applicationContext.getBean(B.class));

    }
    @Slf4j
    @Configuration
    static class BasicConfig{
        @Bean(name= "beanA")
        public A a(){
            return new A();
        }
    }

    @Slf4j
    static class A {
        public void helloA(){
            log.info("hello A");
        }
    }

    @Slf4j
    static class B {
        public void helloB(){
            log.info("hello B");
        }
    }
}
  • new AnnotationConfigApplicationContext(BasicConfig.class)
  • 스프링 컨테이너를 생성하면서 BasicConfig.class 를 넘겨주었다.
  • BasicConfig.class 설정 파일은 스프링 빈으로 등록된다.
 

BeanFactory와 ApplicationContext

1. BeanFactory 스프링 컨테이너의 최상위 인터페이스 = 가장 기초적인 스프링 컨테이너의 역할? 스프링 빈을 관리하고 조회하는 역할 getBean() 제공 2. ApplicationContext BeanFactory기능을 모두 상속받음 그

bsh-developer.tistory.com

 

  • A a = applicationContext.getBean("beanA", A.class)
    • beanA 라는 이름으로 A 타입의 스프링 빈을 찾을 수 있다.
  • applicationContext.getBean(B.class)
    • B 타입의 객체는 스프링 빈으로 등록한 적이 없기 때문에 스프링 컨테이너에서 찾을 수 없다.

 

2. GitHub : 220102 Bean Post Processor


 

GitHub - bsh6463/Spring_Advanced_2

Contribute to bsh6463/Spring_Advanced_2 development by creating an account on GitHub.

github.com

 

Comments