一、首先初始化SpringIOS容器不能使用:ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
那么Web中如何對SpringIOS進行初始化呢。
通過監聽啟動web項目,將SpringIOC容器初始化。
在之前開發Spring項目所必需的的6個jar之外,再導入Spring-web.jar

web項目啟動時會自動加載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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringWebProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<!-- 監聽器的父類ContextLoader中有一個屬性contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!-- 配置spring-web.jar提供的監聽器,可以在服務器啟動時初始化IOC容器 -->
<!-- 初始化IOC容器,(applicationContext.xml),必須告訴監聽器此容器的位置。 -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
創建
文件。
命名為:applicationContext.xml。
啟動tomcat自動實例化IOC容器。
