티스토리 뷰

스터디에서 작게 프로젝트를 생성해서 코딩을 하다보니 ApplicationContext라는 객체를 사용해서 해당 Interface Ben Type에 해당하는 모든 객체들을 아래와 같이 가져오게 되었다.

private final ApplicationContext applicationContext;
	
@PostConstruct
public void init() {
	Collection<Test> makersCollection = applicationContext.getBeansOfType(Test.class).values();
    	...
}

 

ApplicationContext를 통해서 Spring 컨테이너에 등록된 빈들을 가져올 수 있다는 것을 이해하고 이렇게 사용한 것이지만 ApplicationContext란 무엇일까?

 

 

ApplicationContext란?

ApplicationContext는 해당 애플리케이션에 대한 구성정보를 제공하는 인터페이스이다. 애플리케이션에 대한 구성정보라는 것은 등록한 Bean에 대한 또한 포함이어서 위와 같이 특정 타입의 Bean의 리스트(Test 객체를 상속 받은 하위 객체까지 전부)를 가져온다거나 하는 등 Bean의 정보를 가져올 수 있다.

또한 BeanFactory 인터페이스를 상속 받은 인터페이스이다..

 

 

BeanFactory와의 차이점

BeanFactory 또한 해당 애플리케이션에 대한 구성정보를 제공하는 인터페이스이며 등록한 Bean에 대한 정보들이 들어 있다.

그렇다면 ApplicationContext는 BeanFactory를 상속받아서 새로 만들어진 인터페이스인만큼 어떤 차이점이 잇을까?

바로 BeanFactroy는 lazy-loading(지연로딩) 방식이라는 것이다. 지연 로딩은 해당 Bean을 호출할 때에야 Bean이 인스턴스화 되는 것으로 많은 Bean이 등록되어 있는 경우에 필요한 Bean을 그 때 그 때 인스턴스화 해 pre-loading보다 속도가 빠르다.

 

반면에 ApplicationContext는 pre-loading(사전로딩) 방식이어서 해당 Bean에 대한 설정 등을 스프링 컨테이너에서 이미 로드했을 때 Bean이 인스턴스화 되고 ApplicationContext에서 Bean을 호출할 때 기존에 인스턴스화된 Bean을 호출한다. 많은 Bean이 등록되어 있는 시스템일수록 더더욱 강점을 보일 수 있다.

 

(lasy-loading, pre-loading 서로 반대로 써서 수정했습니다.)

 

 

 

✋ ApplicationContext 공식 문서

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html

 

ApplicationContext (Spring Framework 5.3.9 API)

Expose AutowireCapableBeanFactory functionality for this context. This is not typically used by application code, except for the purpose of initializing bean instances that live outside of the application context, applying the Spring bean lifecycle (fully

docs.spring.io

 

 

 

 

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   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
글 보관함