src/main/webapp/WEB-INF/spring/appServlet 아래에 있는 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">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<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="org.jeskey.todo.controller" />
</beans:beans>
annotation-driven: 스프링 MVC 설정을 어노테이션 기반으로 처리한다는 의미
resource: 정적 파일이 있는 폴더 지정
InternalResourcesViewResolver 클래스: 스프링 MVC에서 제공하는 view를 어떻게 결정하는지에 대한 설정 담당. jsp 파일이 있는 폴더 지정
component-scan: 지정된 패키지를 스캔해서@Controller어노테이션이 추가된 클래스들의 객체들을 스프링의 Bean으로 설정한다.
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://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">
(생략)
<mybatis-spring:scan base-package="org.jeskey.todo.mapper"></mybatis-spring:scan>
<!-- 추가할 부분 -->
<context:component-scan base-package="org.jeskey.todo.service"></context:component-scan>
</beans>
servlet-context와root-context의context:component-scan차이점
servlet-context는 view와 관련된 빈을 정의하며
root-context는 view와 관련되지 않은 빈을 정의한다.- 만일
servlet-context에서 패키지 범위를org.jeskey.todo로 확장한다 해도,@Service어노테이션이 있는 빈을 스캔하지 못해 에러가 난다.- 따라서
@Service, @Repository등 비지니스 로직과 관련된 빈을 스캔하기 위해서는root-context에 따로component-scan을 지정해줘야 한다.
Mapper는 @Mapper 어노테이션을 붙이지 않아도 동작하는 이유
<mybatis:scan>은 지정한 패키지의 하위에 있는 모든 인터페이스를 매퍼로 로드한다. 그래서 굳이 @Mapper 어노테이션을 붙일 필요가 없는 것이다.
(저런 특성 때문에 패키지 범위를org.jeskey.todo로 확장하면 Service 인터페이스에도 @Mapper가 붙어서 충돌이 일어나 에러가 나는 듯하다)
🔗 참고 블로그
스프링 MVC를 실행하려면 프론트 컨트롤러 역할을 하는 DispatcherServlet을 설정해 주어야 한다. 이 작업은 Web.xml을 이용해서 처리한다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</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>
</web-app>
<servlet>:DispatcherServlet이 로딩할 때servlet-context.xml을 이용하도록 설정한다.
load-on-startup: 톰캣 로딩 시에 클래스를 미리 로딩함
<servlet-mapping>:DispatcherServlet이 모든 경로의 요청에 대한 처리를 담당하기 때문에/로 지정한다.

톰캣으로 프로젝트를 실행하면 마지막 줄에 DispatcherServlet이 appServlet이라는 이름이로 초기화되었다는 메세지가 출력되는 것을 확인할 수 있다.
추가로 한글 인코딩 설정을 위해
<web-app>태그 안에 필터 설정을 추가한다.
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<servlet-name>appServlet</servlet-name>
</filter-mapping>