728x90
반응형
과거에는 Srping에 xml형식으로 모든 설정을 했다.
현재는 Annotation방식(@)으로 바뀌지만, 여전히 xml로 설정이 가능하다.
레거시 프로젝트를(spring 4 기준) xml형식으로 만들어보면서 spring에 대해 더 깊게 알아보자.
xml은 일반적으로 크게 이런식으로 나눈다.
1. web.xml
2. dispatcher-servlet
3. context-*.xml
위의 3가지는 용도에 따라 나눠둔 것일 뿐 모두 그냥 bean을 등록하는 행위이다.
파일의 시작은 web.xml이다. spring에 기본적으로 webapp/WEB-INF아래의 web.xml을 먼저 보도록 되어있다.
그냥 용도에 따라 나눈 것 뿐이므로 이름을 바꿔도 된다.
dispatcher-servlet.xml은 dispatcher servlet의 역할과 관련된 bean들을 모아놓은 곳이고,
context-datasource는 DB와 관련된 bean설정, context-mapper.xml 은 mapper관련 bean설정 뭐 이런식이다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>gnbis</display-name>
// context-*인 xml파일을 등록
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/context-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
// dispatcher-servlet.xml을 등록
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
이렇게 web.xml의 하위로 xml들을 등록해주면, 각 xml의 용도에 맞는 bean들을 등록해주면 된다. 예를 들어 아래와 같다.
dispatcher-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 어노테이션등록 -->
<context:component-scan base-package="com.gnbis">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:order="2"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
<!-- <mvc:resources mapping="/resources/**" -->
<!-- location="/public" -->
<!-- /> -->
</beans>
context-common.xml
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<context:component-scan base-package="com.gnbis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
728x90
반응형
'Spring' 카테고리의 다른 글
[Spring] sqlExceptionHelper() is null 에러 (0) | 2024.07.01 |
---|---|
[Spring] 정적파일 경로 (0) | 2024.04.05 |
[springboot] springboot 3.0의 새로운 점! (1) | 2024.01.11 |
[spring] swagger-fox 연동하기 (0) | 2023.08.04 |
[springboot] ApplicationPidFileWriter 란? (0) | 2023.08.03 |