Written on
·
191
·
Edited
0
ApplicationContext(Spring Container)는 Interface로 되어있고 우리가 흔히알고있는
AnnotationConfigApplicationContext만 스프링 컨테이너라고 알고있었습니다. 그런데 강의를 듣던도중에 servlet에 대한 AnnotationConfigApplicationContext를 찾아보니 AnnotationConfigWebApplicationContext에 대한 spring container도 찾아볼수있었습니다
그래서 AnnotationConfigApplicationContext == AnnotationConfigWebApplicationContext 인거같아 공식문서를 봐보니
<p>This is essentially the equivalent of {@link org.springframework.context.annotation.AnnotationConfigApplicationContext * AnnotationConfigApplicationContext} for a web environment.
라는 설명이 나오는군요.
첫번째질문으로, 본질적으로는 같은것인데 http요청이 있을때의 ApplicationContext는 AnnotationConfigWebApplicationContext를 쓰고 단순히 project에서 bean을 등록하고 관리하는 ApplicationContext는 AnnotationConfigApplicationContext를 쓰는거같은데 맞나요?
두번째 질문으로,
protected WebApplicationContext createRootApplicationContext() {
Class<?>[] configClasses = getRootConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(configClasses);
return context;
}
else {
return null;
}
}
/**
* {@inheritDoc}
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
* providing it the annotated classes returned by {@link #getServletConfigClasses()}.
*/
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
Class<?>[] configClasses = getServletConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
context.register(configClasses);
}
return context;
}
root ApplicationContext와 ServletApplicationContext에 대한 코드를 공식문서에서 가져왔는데 root ApplicationContext와 ServletApplicationContext는 부모-자식 관계로 이루어져있다고 나와있습니다. 하지만 서로 WebApplicationContext로 구현이 되어있는데 어떻게 부모-자식 관계가 되는걸까요?
세번째 질문으로. root ApplicationContext는 @Service, @Repositroy의 역할이며 Web ApplicationContext는 @Controller역할이라고 나와있는데 @Service, @Repository, @Controller은 Bean으로 등록되어 Spring container로 관리가 된다고 알고있습니다. 물론 WebApplicationContext도 ApplicationContext를 구현한 spring container이긴 하지만 Servlet에 대한 요청과 다른부가적인 요소를 담당한다고 해서 Servlet Container로써 알고있습니다. 그렇게되면 @Service, @Repository, @Controller은 spring에서 관리가 되는것이아닌 servlet에서 관리가 되는게 아닌지 헷갈려서 질문드립니다.
mvc1편에서 servlet container와 spring container를 다르게 구분해서 사용한다고 알고있었는데 공식문서를 찾아보니 하나의 ApplicationContext에서 사용되는거같아 정확한 구분이 필요할거같아 오랜만에 질문드립니다
Answer 1
1
안녕하세요. 박민성님
1. 이것은 서블릿 컨테이너 환경 안에서 사용할 수 있는 스프링 컨테이너입니다.
서블릿을 사용한다면 AnnotationConfigWebApplicationContext 스프링 컨테이너를 사용해야 합니다. 서블릿 환경이 아니라면 AnnotationConfigApplicationContext를 사용하면 됩니다.
2. 두 번째 질문: Root ApplicationContext와 Servlet ApplicationContext의 부모-자식 관계
스프링 웹 애플리케이션에서는 두 종류의 ApplicationContext가 있을 수 있습니다: 하나는 전체 애플리케이션을 위한 root ApplicationContext이고, 다른 하나는 웹 컴포넌트(예: 서블릿)를 위한 Servlet ApplicationContext입니다.
과거에는 웹과 관련이 없는 곳은 root ApplicationContext를 사용하고, 웹과 관련된 곳은 Servlet ApplicationContext로 분리해서 사용했습니다.
이렇게 분리하면 웹과 관련된 스프링 컨테이너와 웹과 관련이 없는 스프링 컨테이너를 명확하게 분리할 수 있습니다.
하지만 최근에는 이렇게 분리하지 않고 하나의 스프링 컨테이너에 모두 합쳐서 사용합니다. 스프링 부트의 경우에도 하나의 스프링 컨테이너만 만들어서 사용합니다.
따라서 지금은 이 관계에 대해서 크게 고민하지 않아도 됩니다.
3. 아닙니다. 서블릿 컨테이너와 스프링 컨테이너는 명확히 분리되어 있습니다. 개념적으로 보면 웹 애플리케이션의 경우 서블릿 컨테이너 안에서 스프링 컨테이너가 작동합니다. 그래도 @Service, @Repository, @Controller와 같은 스프링 빈들은 모두 스프링 컨테이너 안에서 관리합니다.
감사합니다.
늦은시간에도 답변달아주셔서 감사드립니다 즐거운 설연휴보내세요!