코딩하는 문과생
[Spring] Spring 프레임워크(7) - Context분리와 전략 본문
[Context분리와 전략]
지금까지 하나의 Context를 사용했었다.
그러나 일반적으로 두가지로 나눠서 사용한다.
- Servlet Context: DispatcherServlet(웹의 요청을 최초로 접수, 설정파일을 이용해서 ServletContext 즉, 스프링 컨테이너 로딩), Spring-MVC와 관련 있는 빈(Controllers, ViewResolver, HandlerMapping 등)을 설정
- Root Context: Spring-MVC와 분리되어 빈을 관리하고 싶을때(Services빈이나 Repositories빈 등), 오픈API를 서비스하고 싶은 경우
- web.xml에서 설정이 필요
리스너(listener) 생성(스프링 컨테이너를 별도로 띄우는 역할)
[Root Application Context]
: 전체 계층 구조에서 최상단에 위치한 컨텍스트
- 특징
- 서로 다른 서블릿 컨텍스트에서 공유해야하는 빈들을 등록
- DB연결이나, 로깅기능
- Servlet Context에 등록된 빈 이용 불가능
- 둘다 등록되어 있다면 Servlet Context 빈이 우선된다.
- include-filter 사용
[Servlet Application Context]
- 서블릿에서만 이용되는 컨텍스트
- exclude-filter 사용
ex. Context
-web.xml에 리스너 추가
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 물리적 구분, 그러나 하나의 컨테이너에 올라간다. -->
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param>
-servlet-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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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>
<mvc:annotation-driven/>
<!-- 해당 url로 접근되면 dispatcher가 컨트롤러가 아닌 해당 파일을 찾는다. -->
<mvc:resources location="/WEB-INF/resources/" mapping="/resources/**"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="kr.co.acomp.hello">
<!-- controller 타입은 배체 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
-datasource.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder location="classpath:config/database.properties"/>
<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>
- spring explorer로 확인 가능

'웹 프로그래밍 > Spring' 카테고리의 다른 글
| [Spring] Spring 프레임워크(9) - AOP (0) | 2020.03.12 |
|---|---|
| [Spring] Spring 프레임워크(8) - MyBatis (0) | 2020.03.10 |
| [Spring] Spring 프레임워크(6) - 데이터 접근 기술 (0) | 2020.03.09 |
| [Spring] Spring 프레임워크(5) - Spring Test (0) | 2020.03.09 |
| [Spring] Spring 프레임워크(4) - static 파일처리, 파일 업로드 (0) | 2020.03.08 |