# XML 설정 파일
- 인코딩, 리소스 파일 등의 경로 정보를 설정을 지정하는 파일
- STS 프로젝트 생성 시, XML 파일이 자동으로 생성된다.
[참고] 본 예제의 예시 파일들은 mvc패턴의 설정 파일이다.
# WEB.XML
- Jsp와 컨트롤러간 전송 파라미터의 인코딩 타입을 지정 및 스프링 동작 시 servlet-context.xml의 설정 정보를 읽어들인다.
- DispatcherServlet을 매핑하는 부분이 자동으로 생성
<?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">
<!-- filter속성
- Jsp와 컨트롤러 간 파라미터로 전송 시 .do등의 특정 확장자의 맵핑 주소를 형식으로 사용할 경우
- 맵핑 주소를 특정 인코딩 타입으로 받을 수 있도록 설정하는 속성이다.
- .do로 끝나는 확장자명의 url은 모두 utf-8의 형식으로 인코딩된다.
-->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- root-context 참조 경로
- classpath의 * 표시는 프로젝트 내의 모든 경로를 뜻한다.
- 모든 경로 상을 검색하여 config/spring/context/context-*.xml 로 시작하는 XML파일을 전부 읽는다.
-->
<param-value>classpath*:config/spring/context/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>
<!-- servlet-context의 참조 경로
- config/spring/mvc/servlet-context.xml 파일을 참조한다.
# 동작 순서
- (1) web.xml이 실행되며 root-context의 경로 내 파일을 순차적으로 실행
- (2) config/spring/mvc/servlet-context.xml을 실행
-->
<param-value>classpath:config/spring/mvc/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>
</web-app>
# servlet-context.xml
- Jsp의 위치를 지정하는 InternalResourceViewResolver와 참조(리소스) 파일 경로나 어노테이션 설정 등의 정보가 저장되는 파일
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- resource속성
- 참조파일(이미지, 스크립트 등)의 저장 경로를 webApp의 resource에서 보관한다.
-->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<!-- context:component-scan (auto-detecting)
- 지정한 패키지의 하위 경로에 있는 클래스들을 스캔하여 의존성 주입 어노테이션을 자동으로 인식하고 빈객체를 주입하는 속성
- 클래스를 스캔하는 기준은 어노테이션으로 구분되는데
- (1) @Controller
- (2) @Service
- (3) @Repository
- (4) @Component
- (5) @Autowired
- (6) @Resource 등이 해당된다.
- 자동으로 주입해준다는 장점이 있으나
- 파라미터를 직접 보낼 수 없다느 단점이 존재한다.
[참고] *문자를 통해서 다수의 패키지를 한번에 지정 가능하다.
-->
<context:component-scan base-package="com.korea.*" />
<!-- context:annotation-config
- 의존성 주입 어노테이션을 활성화하는 속성으로
- (1) @Autowired (2) @Resource 등의 어노테이션이 해당된다.
- 해당 속성은 스캔처럼 빈을 직접 찾아서 자동으로 등록하지는 않으므로 @Autowired등의 어노테이션이 없다면 빈객체를 등록하는 클래스에서 셋터 혹은 생성자로 받아줘야한다.
-->
<context:annotation-config/>
</beans:beans>
'Web > Spring' 카테고리의 다른 글
[Spring] Mybatis (0) | 2025.01.09 |
---|---|
[Spring] Restful API (0) | 2025.01.09 |
[Spring] 관점지향 프로그래밍 (0) | 2025.01.09 |
[Spring] 어노테이션 (0) | 2025.01.09 |
[Spring] MVC 디자인 패턴 (0) | 2025.01.09 |