1. 引言
在開發 Spring 應用時,需要告知框架 Bean 的查找位置。應用程序啓動時,框架會定位並註冊所有 Bean 以供後續執行。同樣地,我們也需要定義所有傳入 Web 應用程序的請求將被處理的映射。
所有 Java Web 框架都建立在 Servlet API 之上。在 Web 應用程序中,三個文件起着至關重要的作用:通常情況下,我們按照以下順序鏈接它們:web.xml -> applicationContext.xml -> spring-servlet.xml
在本文中,我們將探討 applicationContext 和 spring-servlet 之間的差異。
2. applicationContext.xml
控制反轉 (IoC) 是 Spring 框架的核心。在 IoC 啓用框架中,通常一個容器負責實例化、創建和銷燬對象。在 Spring 中,applicationContext 扮演着 IoC 容器的角色。
當開發標準 J2EE 應用程序時,我們在 web.xml 文件中聲明 ContextLoaderListener。此外,我們還定義 contextConfigLocation 以指示 XML 配置文件的位置。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>當應用程序啓動時,Spring 會加載此配置文件並使用它創建 WebApplicationContext 對象。在缺少 contextConfigLocation 的情況下,默認情況下,系統會查找 /WEB-INF/applicationContext.xml 以加載它。
簡而言之,applicationContext 是 Spring 中的核心接口。它為應用程序提供配置信息。
在此文件中,我們提供與應用程序相關的配置,通常包括基本數據源、佔位符文件以及項目本地化消息源等增強功能。
讓我們來看一個示例文件:
<?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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="classpath:/database.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
</beans>ApplicationContext 是 BeanFactory 接口的完整超集,因此它提供了 BeanFactory 的所有功能。它還提供了集成的生命週期管理、BeanPostProcessor 和 BeanFactoryPostProcessor 的自動註冊、便捷的 MessageSource 訪問以及 ApplicationEvent 的發佈。
3. <em >spring-servlet.xml</em >
在 Spring 中,一個前置 Servlet 負責處理傳入的請求並將其委託給適當的控制器方法。該前置 Servlet 基於 Front Controller 設計模式,處理特定 Web 應用程序的所有 HTTP 請求。該前置 Servlet 對所有傳入請求擁有控制權。
同樣,<em >spring-servlet</em > 作為 Front Controller Servlet 充當單點入口,它接收傳入的 URI。在幕後,它使用 `HandlerMapping> 實現來定義請求與 Handler 對象的映射關係。
以下是示例代碼:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.baeldung.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>4. applicationContext.xml 與 spring-servlet.xml
下面我們來看概要視圖:
| Feature | applicationContext.xml | spring-servlet.xml |
|---|---|---|
| Framework | 它是 Spring 框架的一部分。 | 它是 Spring MVC 框架的一部分。 |
| Purpose | 一個定義 Spring Beans 的容器。 | 一個前控制器,用於處理傳入的請求。 |
| Scope | 它定義了在所有 Servlet 中共享的 Bean。 | 它只定義 Servlet 相關的 Bean。 |
| Manages | 它管理全局事物,如 datasource 和連接工廠都定義在其中。 | 相反,只有與 Web 相關的事物,如控制器和 viewresolver 將定義在其中。 |
| References | 它不能訪問 spring-servlet 的 Bean。 | 它可以訪問 applicationContext 中定義的 Bean。 |
| Sharing | 在這裏定義整個應用程序的公共屬性。 | 在這裏定義僅特定 Servlet 的屬性。 |
| Scanning | 我們定義用於包含/排除包的過濾器。 | 我們聲明控制器掃描。 |
| Occurrence | 通常在應用程序中定義多個上下文文件。 | 同樣,我們可以在 Web 應用程序中定義多個文件。 |
| Loading | ContextLoaderListener 加載 applicationContext.xml 文件。 | DispatcherServlet 加載 spring-servlet.xml 文件。 |
| Required | Optional | Mandatory |
5. 結論
在本教程中,我們學習了 <em >applicationContext</em> 和 <em >spring-servlet</em> 文件。 隨後,我們討論了它們在 Spring 應用中的作用和職責。 最後,我們比較了它們之間的差異。