web.xml, root-context.xml, servlet-context.xml 에서 스프링 관련 설정 파일들을 빈으로 주입 시켜주는데 상황에 따라 xml 설정 파일에 주입 위치가 달랐다. 서로 어떤 차이가 있는지 궁금했다.
web.xml
WAS가 최초로 구동시킬떄 web.xml파일을 읽어 각종 설정들을 정의해준다. web.xml 파일에는 주로 이러한 설정을 해준다. DispatcherServlet, Filter , ContextLoaderListener,
<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>
<!-- 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>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1. DispatcherServlet 클라이언트의 요청을 처리하는
DispatcherServlet을 등록해주면서 스프링 설정 파일을 지정한다.
DispatcherServlet은 초기화 과정에서 지정된 설정 파일을 이용해 스프링 컨테이너를 초기화시킨다.
2. Filter 거쳐야 하는 필터
클라이언트에서 온 요청을 Dispatcher Servlet이 받기 전 거치는 부분이 있다. 바로 이 Filter 객체이다.
만약 스프링 시큐리티 필터가 적용되어 있다면, 인가 및 인증 처리를 먼저 처리하고, 인코딩 필터가 적용되어 있다면 클라이언트의 요청데이터를 인코딩하는 작업이 선 처리된 후 Dispatcher Servlet에게 필터링 된 데이터가 전달된다.
3. ContextLoaderListener 웹 어플리케이션 컨텍스트 단위의 설정을 로드하는
ContextLoaderListener 와 DispatcherServlet 은 각각 webApplicationConttext 인스턴스를 생서하는데,
ContextLoaderListener 가 생성한 컨텍스느가 root 컨텍스트가 되고 , DispatcherServlet 생성한 인스턴스는 root 컨텍스트를 부모로 사용하는 자식 컨텐스트가 된다.
흐름을 보면 web.xml에서 ContextLoaderListener를 이용해 root-context를, DispacherServlet을 이용하여 servlet-context를 생성한다
root-context.xml?
view와 관련되지않은 비지니스 로직 관련을 설정하는 곳이다 주로 데이터베연결, service, repository(dao) 설정이 들어간다.
- 이곳에 등록되는 빈은 모든 컨텍스트에서 사용할 수 있다(공유가능).
- service나 dao를 포함한, 웹 환경에 독립적인 빈들을 담아둔다.
- 서로 다른 servlet-context에서 공유해야하는 빈들을 등록해놓고 사용할 수 있다.
- servlet-context 내의 빈들은 이용이 불가능하다
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
<property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3306/book_ex"></property>
<property name="username" value=""></property>
<property name="password" value=""></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<context:component-scan base-package=""></context:component-scan>
<context:component-scan base-package=""></context:component-scan>
servelet-context.xml (웹과 관련된 스프링 설정파일)
요청과 관련된 객체를 정의한다. controller, 어노테이션, viewResolver 프론트적인 부분 설정을 하는 파일 인거같다
- 이곳에 등록되는 빈은 해당 컨텍스트에서만 사용할 수 있다.
- DispatcherServlet이 직접 사용하는 컨트롤러를 포함한 웹 관련 빈을 등록하는데 사용한다.
- 독자적인 컨텍스트를 가지며, root-context내의 빈을 사용할수 있다.
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/**" 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>
<context:component-scan base-package="com.fastcampus.ch3">
<context:exclude-filter type="regex" expression="com.fastcampus.ch3.diCopy*.*"/>
</context:component-scan>
<view-controller path="/" view-name="index"/>
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basenames">
<beans:list>
<beans:value>error_message</beans:value> <!-- /src/main/resources/error_message.properties -->
</beans:list>
</beans:property>
<beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>
</beans:beans>
'Develop > Spring' 카테고리의 다른 글
[Spring] Bean/component-scan 동작 과정 (0) | 2024.08.29 |
---|---|
[Spring] Spring vs Springboot 차이 (0) | 2024.06.02 |
[Spring] DAO,DTO,VO 알아보기 (0) | 2024.03.13 |
[Spirng] Spring MVC 프로젝트 폴더 구조와 실행 순서 and DispatcherServlet 소스 분석 (0) | 2024.02.20 |
[Spirng] 스프링 @ExceptionHandler와 @ControllerAdvice 사용해 예외처리 방법 and @ResponseStatus (0) | 2024.02.19 |