1、简介 {#1简介}
所有 Java Web 框架都建立在 Servlet Api 的基础之上。在基于 Spring 开发的 Java Web 应用中,有三个文件起着至关重要的作用。通常,按以下顺序将它们串联起来:web.xml
-> applicationContext.xml
-> spring-servlet.xml
。
本文将带你了解 applicationContext.xml
和 spring-servlet.xml
之间的区别。
2、applicationContext.xml {#2applicationcontextxml}
反转控制(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 的核心接口。它为应用提供配置信息。
在该文件中,提供了与应用相关的配置。通常,这些配置包括基本数据源、属性占位符文件(Property Place Holder)和用于项目本地化的消息源(Message Source),以及其他增强功能。
示例如下:
<?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、spring-servlet.xml {#3spring-servletxml}
在 Spring 中,一个前端 Servlet 接收传入的请求,并将其委托给适当的 Controller 方法。前端 Servlet 基于 "前端控制器设计模式",处理特定 Web 应用的所有 HTTP 请求。该前端 Servlet 拥有对传入请求的所有控制权。
spring-servlet
充当前端控制器 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 {#4applicationcontextxml-和-spring-servletxml}
区别汇总如下:
| 特性 | applicationContext.xml | spring-servlet.xml |
|----------|----------------------------------------------------------|-------------------------------------------------------------|
| 架构 | Spring 框架的一部分 | Spring MVC 框架的一部分 |
| 作用 | 定义 Spring Bean 的容器 | 前端控制器,负责处理接收到的请求 |
| 范围 | 定义了所有 Servlet 共享的 Bean | 只定义 Servlet 专用的 bean |
| 管理职责 | 它管理 datasource
源等全局事物,连接工厂也在其中定义 | 相反,只有控制器(controller)和视图解析器(viewresolver)等与 Web 相关的内容才会在其中定义 |
| 可访问性 | 无法访问 spring-servlet
的 Bean | 可以访问 applicationContext
中定义的 Bean |
| 属性共享 | 整个应用共有的属性放在这里 | 只适用于特定 Servlet 的属性将放在这里 |
| 组件扫描 | 定义 filter 来包含/排除 package | 为 controller 声明组件扫描 |
| 场景 | 在应用中定义多个 Context 文件 | 同样,也可以在 Web 应用中定义多个文件 |
| 加载 | 通过 ContextLoaderListener
加载applicationContext.xml
文件 | 通过 DispatcherServlet
加载 spring-servlet.xml
|
| 必须 | 可选 | 强制 |
5、总结 {#5总结}
本文介绍了 applicationContext.xml
和 spring-servlet.xml
文件在 Spring 应用中的作用和职责以及它们之间的区别。
总的来说,applicationContext.xml
是父容器,定义应用中全局共享的组件,如:数据源、业务层、持久层等等。而 spring-servlet.xml
是子容器,定义和 Mvc 相关的组件,如:Controller、视图解析器、处理器映射器等等。子容器可以访问父容器的资源,反之则不行,例如:可以在 Controller 中注入 Service 组件,但是反过来就会注入失败。
Ref:https://www.baeldung.com/spring-applicationcontext-vs-spring-servlet-xml