본문 바로가기

spring/Web

공유자원 설정할때 사용(ContextLoaderListener : applicationContext.xml)

기본적으로 셋팅되어야할 설정이 필요할때 사용한다.


context-param 태그로 설정파일을 명시하지 않으면 applicationContext.xml 파일이 설정파일로 적용된다.


단, context-param 태그를 명시하지 않을 경우에도 listner 태그를 이용해서 ContextLoaderListener class를 명시해줘야한다.


ContextLoaderListener 설정 파일은 servlet 설정 파일보다 먼저 실행되며 


따라서 ContextLoaderListener 설정 파일에서 설정된 값을 서블릿에서 사용할수 있게된다.


============================================================================================================================================

web.xml


<?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_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>s_mvc4_share</display-name>

  

        <!-- 공유자원 설정할때 사용(DispatcherServlet 보다 먼저 실행됨) -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/app.xml</param-value>

</context-param>


<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

  

  

  <servlet>

  <servlet-name>hello</servlet-name> <!-- servlet name이 설정파일 이름이됨 : hello-servlet.xml -->

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>hello</servlet-name>

  <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  

  <servlet>

  <servlet-name>world</servlet-name> <!-- servlet name이 설정파일 이름이됨 : world-servlet.xml -->

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <load-on-startup>2</load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>world</servlet-name>

  <url-pattern>*.htm</url-pattern>

  </servlet-mapping>

</web-app>


============================================================================================================================================

ContextLoaderListener class 설정 파일


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="sharedData" class="pack.SharedData">

<property name="shared" value="굿잡"/>

</bean>

</beans>


============================================================================================================================================

hello-servlet.xml 일부


<bean name="/hello.do" class="pack.HelloController">

<property name="data" ref="sharedData"></property>

</bean>



============================================================================================================================================

world-servlet.xml 일부


<bean name="/world.htm" class="pack.WorldController">

<property name="data" ref="sharedData"></property>

</bean>