스프링 설정 파일 나누기 
(dispatcher-servlet.xml 분리)


스키마 설정만 남기고 새로운 폴더에 원하는 카테고리로 설정 파일을 나눈다

 


ex)
servlet-context 껍데기
service-context 데이터 서비스
security-context 로그인, 보안 관련 서비스

담당 업무 분류로 사용할 수도 있다
(공유 repository에 올리면 파일이 겹치지 않으니까)
-비동기 개발 작업

설정 파일을 직관적으로 파악도 가능함


[service-context.xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="noticeService" class="com.newlecture.web.service.jdbc.JDBCNoticeService">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521/xepdb1" />
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
   
</beans>


이런 식으로 관련된 설정 코드만 넣는다
기존에 사용하던 dispatcher-servlet.xml 은 확장자를 .backup으로 바꿔주던 지우던 한다

dispatcher를 맵핑해주던 web.xml로 이동


[web.xml]
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/spring/service-context.xml
    /WEB-INF/spring/security-context.xml
  </param-value>
 </context-param>
 
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

 

(굵게 칠해진 부분이 추가되는 부분이다)

<init-param> 안에는 한 개의 param 밖에 넣을 수 없다

<listener>
톰캣 or 세션이 시작할 때, 끝날 때 해결할 수 있는 이벤트를 처리할 때 사용됨
<listener>의 <context-param>값을 DispatcherServlet가 이용할 수 있으므로 추가하지 못한 설정 파일 경로를 추가한다

<load-on-startup>1</load-on-startup>
dispatcher(servlet객체)는 최초에 url을 호출 받았을 때 메모리에 올라가기 때문에 첫 요청시 속도가 느리다. 설정인 녀석이 느리면 안되므로 요청이 오기전에 미리 메모리에 첫번째로 올라갈 수 있도록 해준다.
load는 tomcat 서버의 load를 뜻한다. 서버가 올라갈 때 1번으로 올라가도록 설정.

<async-supported>true</async-supported>
비동기 설정이다. 동기적으로 다른 프로세스를 기다리지 않고 비동기화하여 바로 로드한다.

+ Recent posts