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>
애플리케이션 전체에서 공유되는 빈들을 정의한다. 이는 서비스, 데이터베이스 연결, 보안 설정 등과 같이 애플리케이션 전반에 걸쳐 사용되는 빈을 포함한다.
<?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><context:property-placeholder>웹 계층과 관련된 빈들을 정의한다. 이는 컨트롤러, 뷰 리졸버, 인터셉터 등과 같이 웹 요청을 처리하는 데 필요한 빈을 포함한다.
<?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만 띄워줘야 하는 경우가 있다. 그럴 때 이 태그를 사용하면 된다.
애플리케이션 전체에서 공유되는 빈과 그렇지 않은 빈을 분리하여 코드를 보다 체계적으로 구성할 수 있기 때문이다.