root context? servlet context?

trankill_Kim·2024년 4월 21일

context란? 스프링 빈들을 관리하는 컨테이너


web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  
  <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>

root-context란?

애플리케이션 전체에서 공유되는 빈들을 정의한다. 이는 서비스, 데이터베이스 연결, 보안 설정 등과 같이 애플리케이션 전반에 걸쳐 사용되는 빈을 포함한다.

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://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="com.ssafy.model"></context:component-scan>
	<context:property-placeholder location="classpath:db.properties"/>
	
<!-- 	<bean id="ds" class="org.apache.commons.dbcp2.BasicDataSource" 
		p:driverClassName="com.mysql.cj.jdbc.Driver"
		p:url="jdbc:mysql://localhost:3306/scott?serverTimeZone=UTC"
		p:username="ssafy"
		p:password="ssafy"
		/> -->
		
	<bean id="ds" class="com.zaxxer.hikari.HikariDataSource" 
		p:driverClassName="${db.driver}"
		p:jdbcUrl="${db.url}"
		p:username="${db.username}"
		p:password="${db.password}"
		/>
	
</beans>
  • <context:component-scan>
    특정 패키지 내의 클래스를 스캔하고 Annotation을 확인하여 Bean 인스턴스를 생성한다.
  • <context:property-placeholder>
    public 저장소에 저장하기 부담스러운 DB 접속 정보, API key 등의 값들은 .properties와 같은 외부 설정값을 관리하는 파일에 저장하여 관리한다. 이러한 외부 설정값 파일을 로드할 때 사용하는 태그다.

servlet-context란?

웹 계층과 관련된 빈들을 정의한다. 이는 컨트롤러, 뷰 리졸버, 인터셉터 등과 같이 웹 요청을 처리하는 데 필요한 빈을 포함한다.

<?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">

	<annotation-driven />

	<context:component-scan base-package="com.ssafy.controller" />

	<view-controller path="/" view-name="index"/>
	<view-controller path="/dept/registerForm" view-name="deptRegister"/>

	<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>
	
	<resources mapping="/resources/**" location="/resources/" />
	
</beans:beans>
  • <annotation-driven>
    Spring MVC @Controller에 요청을 보내는데 필요한 HandlerMapping과 HandlerAdapter를 Bean으로 등록해준다.
    ㄴ HandlerMapping : HTTP 요청 정보를 이용해서 컨트롤러를 찾아준다.
    ㄴ HandlerAdapter : HandlerMapping을 통해 찾은 컨트롤러를 직접 실행한다.

  • <view-controller>

    스프링 프레임워크를 사용하여 코드를 짜다보면 오로지 jsp만 띄워줘야 하는 경우가 있다. 그럴 때 이 태그를 사용하면 된다.


왜 이렇게 나눠서 관리해야 하는가?

애플리케이션 전체에서 공유되는 빈과 그렇지 않은 빈을 분리하여 코드를 보다 체계적으로 구성할 수 있기 때문이다.

0개의 댓글