본문 바로가기

Archived(Programming)/Spring #2(기초)

Context 분리와 전략

# Servlet Context

DispatcherServlet - 웹의 요청을 최초로 접수(프론트 컨트롤러)

DispatcherServlet - 설정파일을 이용해서 ServletContext(스프링 컨테이너) 로딩

Spring MVC와 관련있는 빈을 설정

 

# Root Context

Spring MVC와 분리되어 빈을 관리하고 싶을 때 사용

일반적으로 Servlet Context와 Root Context 로 분리하여 운용

 

Spring MVC Context 계층 구조(단방향만 참조 가능)

# Root Application Context

  • 전체 계층 구조에서 최상단에 위치한 컨텍스트
  • 서로 다른 서블릿 컨텍스트에서 공유해야 하는 Bean들을 등록해놓고 사용
  • 웹 어플리케이션 전체에 적용가능한 DB 연결, 로깅 기능 등에 이용
  • Servlet Context에 등록된 Bean 이용 불가능
  • Servlet Context와 동일한 Bean이 있을 경우 Servlet Context Bean이 우선됨
  • 하나의 컨텍스트에 정의된 AOP 설정은 다른 컨텍스트의 빈에는 영향을 미치지 않음

Root context

# Servlet Context

  • 서블릿에서만 이용되는 컨텍스트
  • 타 서블릿과 공유하기 위한 Bean들은 Root Web Application Context에 등록해놓고 사용
  • DispatcherServlet은 자신만의 컨텍스트를 생성, 초기화하고 동시에 루트 어플리케이션 컨텍스트를 찾아서 자신의 부모 컨텍스트로 사용

Servlet Context
Spring Explorer > Context에 로딩된 Bean 확인

# 실습 1. Context 파일 분리

(참고: Spring MVC project로 생성 시 자동으로 분리되어있음)

 

web.xml

...
	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-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>

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"
	xmlns:context="http://www.springframework.org/schema/context">
	
	<context:component-scan base-package="kr.co.acomp.hello">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	<context:property-placeholder location="classpath:config/database.properties"/>
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${db.driverClass}" />
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
	</bean>
	
</beans>

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="kr.co.acomp.hello"
			use-default-filters="false">
		<context:include-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<beans:property name="maxUploadSize" value="104857600" />
		<beans:property name="defaultEncoding" value="UTF-8" />
	</beans:bean>	
	
	
	
</beans:beans>

Spring Explorer 확인

# 실습 2. 여러 개의 Root-context.xml 파일 분리

web.xml

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring/root-context.xml
			/WEB-INF/spring/datasource.xml
		</param-value>
	</context-param>

datasource.xml 파일생성 - hello-web/src/main/webapp/WEB-INF/spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
	xmlns:context="http://www.springframework.org/schema/context">

	<context:property-placeholder location="classpath:config/database.properties"/>
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${db.driverClass}" />
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
	</bean>
</beans>

root-context.xml 파일 수정

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"
	xmlns:context="http://www.springframework.org/schema/context">
	
	<context:component-scan base-package="kr.co.acomp.hello">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
</beans>

Spring Explorer 확인

 

'Archived(Programming) > Spring #2(기초)' 카테고리의 다른 글

Mapper XML 파일  (1) 2020.03.16
Spring Mybatis  (0) 2020.03.15
Spring Data Access 기술과 DataSource  (0) 2020.03.13
JUnit4 & Spring Test  (0) 2020.03.13
Static file 처리와 FileUpload  (0) 2020.03.12