Spring Container & Bean

dazzi·2023년 8월 10일

Spring

목록 보기
1/5
post-thumbnail
                                        
ApplicationContext applicationContext 
	= new AnnotationConfigApplicationContext(AppConfig.class);

✅ ApplicationContext

= Spring Container
+ Interface

✅ new AnnotationConfigApplicationContext(AppConfig.class)

+ ApplicationContext Interface의 구현체

✅ AppConfig.class

+ 구성 정보

📌 즉, 지정한 구성정보를 가진 - 내부의 빈(Bean) 저장소가 있는 - 스프링 컨테이너가 생성된다!

✅ @Bean

+ 스프링 빈 등록

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

+ 빈 이름 : memberService
+ 빈 객체 : MemberServiceImpl@x01

🔔 빈 이름은 항상 다른 이름 부여!!!

cf) 스프링은 빈을 생성하고 의존관계를 주입하는 단계가 나누어져 있음.
이렇게 자바코드로 스프링 빈을 등록하면 생성자를 호출하면서 의존관계 주입(DI)도 한번에 처리됨

✅ 스프링 빈 조회

//1)
ac.getBean(beanName, beanType)
//2)
ac.getBean(beanType)

조회 대상 스프링 빈이 없으면 예외 발생
NoSuchBeanDefinitionException: No bean named 'xxxxx' available

✅ 스프링 빈 조회 - 상속관계

+ 부모타입으로 조회하면, 자식타입도 함께 조회
ex Object type으로 조회하면, 모든 스프링 빈을 조회한다.

✅ BeanFactory && ApplicationContext

BeanFactory

  • Spring Container의 최상위 interface
  • Spring Bean을 관리 및 조회
  • getBean() 제공

AppplicationContext

  • BeanFactory 기능 모두 상속
  • 빈을 관리 및 조회 기능 + 부가기능 제공

부가기능

  • 메시지 소스를 활용한 국제화 기능
  • 환경변수
  • 애플리케이션 이벤트
  • 편리한 리소스 조회

✅ 스프링 빈 메타 정보 - BeanDefinition

  • 추상화
  • 역할구현을 개념적으로 나눔
  • 스프링 컨테이너는 오직 BeanDefinition만 알면 됨(자바코드인지, XML인지 몰라도 됨)
  • @Bean, 당 각각 하나씩 메타 정보가 생성
  • 스프링 컨테이너는 이 메타정보를 기반으로 스프링 빈을 생성

0개의 댓글