상세 컨텐츠

본문 제목

[Spring] ApplicationContext / WebApplicationContext 개념

Spring/개념

by Chan.94 2022. 3. 21. 10:45

본문

반응형

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/context-*.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

설정파일을 보게되면 ApplicationContextWebApplicationContext 두 부분에 대한 내용을 확인 할 수 있다.

 

ApplicationContext는 ContextLoaderListener 클래스에 의해 만들어지고, WebApplicationContext는 DispatcherSerlvet 클래스에 의해 만들어진다.

 

 

 

ApplicationContext와 WebApplicationContext의 관계를 도식화 한 자료이다.

 

ApplicationContext

  • 최상위 컨텍스트
  • root-context에 등록되는 빈들은 모든 컨텍스트에서 사용 할 수 있다. (공유가능)
  • Service, DAO를 포함한 웹환경에서 독립적인 Bean들을 담아둔다. (@Service, @Repository)
  • 서로 다른 servlet-context에서 공유해야 하는 빈들을 등록해놓고 사용할 수 있다.
  • servlet-context내 Bean들은 이용이 불가능하다.

 

xml

<context:component-scan base-package="패키지경로">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

component-scan의 설정을 통해 Service, DAO를 Bean으로 등록해준다.

 

 

WebApplicationContext

  • servlet-context에 등록되는 빈들은 해당 컨텍스트에서만 사용할 수 있다.
  • DispatcherServlet이 직접 사용하는 Controller를 포함한 웹 관련 빈을 등록하는 데 사용한다. (@Controller)
  • 독자적인 컨텍스트들을 가지며, root-context 내 빈 사용이 가능합니다.

 

xml

<context:component-scan base-package="패키지경로">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

component-scan의 설정을 통해 Controller를 Bean으로 등록해준다.


context란?

- Spring이 관리하는 bean들이 담겨있는 컨테이너를 말한다.

[Spring & Spring Boot/개념] - [Spring] IoC 컨테이너 / Bean

반응형

'Spring > 개념' 카테고리의 다른 글

REST API 개념  (0) 2022.07.13
annotation-driven / component-scan / annotation-config 차이 정리  (1) 2022.03.28
[JAVA] Multi-Thread 사용법  (0) 2022.03.02
[Spring] IoC 컨테이너 / Bean  (0) 2021.10.14
Spring AOP (Proxy)  (0) 2021.10.08

관련글 더보기

댓글 영역

>