1. 程式人生 > >自動啟動Spring容器的配置

自動啟動Spring容器的配置

<?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

id="WebApp_ID" version="2.4">

<!--1.從類路徑下載入Spring配置檔案,classpath關鍵字特指類路徑下載入-->       //注意:類路徑表示src目錄

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!--2.負責啟動Spring容器的監聽器,它將引用1處的上下文引數獲得Spring配置檔案地址-->

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

</web-app>

首先,通過Web容器上下文引數指定Spring配置檔案的地址,如1所示。多個配置檔案可用逗號或空格分隔,建議採用逗號分隔的方式。在2處指定Spring所提供的ContextLoaderListener的Web容器監聽器,該監聽器在web容器啟動時自動執行,它會根據contextConfigLoacation Web容器引數獲取Spring配置檔案,並啟動Spring容器。注意需要將log4j.properties日誌配置檔案放置在類路徑下,以便日誌引擎自動生效