1. Introduction
1.绪论
When developing a Spring application, it is necessary to tell the framework where to look for beans. When the application starts, the framework locates and registers all of them for further execution. Similarly, we need to define the mapping where all incoming requests to the web application will be processed.
当开发一个Spring应用程序时,有必要告诉框架在哪里寻找Bean。当应用程序启动时,框架会定位并注册所有的bean,以便进一步执行。同样地,我们需要定义映射,所有进入Web应用程序的请求都将在这里被处理。
All the Java web frameworks are built on top of servlet api. In a web application, three files play a vital role. Usually, we chain them in order as: web.xml -> applicationContext.xml -> spring-servlet.xml
所有的Java网络框架都建立在servlet api之上。在一个Web应用程序中,有三个文件起着至关重要的作用。通常情况下,我们将它们按照以下顺序进行连锁。web.xml -> applicationContext.xml -> spring-servlet.xml。
In this article, we’ll look at the differences between the applicationContext and spring-servlet.
在这篇文章中,我们将看看applicationContext和spring-servlet的区别。
2. applicationContext.xml
2.applicationContext.xml
Inversion of control (IoC) is the core of the Spring framework. In IoC enabled framework, usually, a container is responsible for instantiating, creating, and deleting objects. In Spring, applicationContext plays the role of an IoC container.
反转控制(IoC)是Spring框架的核心。在启用了IoC的框架中,通常,一个容器负责实例化、创建和删除对象。在Spring中,applicationContext就扮演着IoC容器的角色。
When developing a standard J2EE application, we declare the ContextLoaderListener in the web.xml file. In addition, a contextConfigLocation is also defined to indicate the XML configuration file.
在开发标准的 J2EE 应用程序时,我们在 web.xml 文件中声明 ContextLoaderListener。此外,还定义了一个contextConfigLocation来指示 XML 配置文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
When the application starts, the Spring loads this configuration file and uses it to create a WebApplicationContext object. In the absence of contextConfigLocation, by default, the system will look for/WEB-INF/applicationContext.xml to load.
当应用程序启动时,Spring会加载这个配置文件并使用它来创建一个WebApplicationContext对象。如果没有contextConfigLocation,默认情况下,系统将寻找/WEB-INF/applicationContext.xml来加载。
In short, applicationContext is the central interface in Spring. It provides configuration information for an application.
简而言之,applicationContext是Spring的核心接口。它为一个应用程序提供配置信息。
In this file, we provide the configurations related to the application. Usually, those are the basic data source, property place holder file, and message source for project localization, among other enhancements.
在这个文件中,我们提供与应用程序相关的配置。通常,这些是基本数据源、属性放置文件和项目本地化的消息源,以及其他增强功能。
Let’s look at the sample file:
让我们来看看这个样本文件。
<?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 is a complete superset of the BeanFactory interface and, hence, provides all the functionalities of BeanFactory. It also provides the integrated lifecycle management, automatic registration of BeanPostProcessor and BeanFactoryPostProcessor, convenient access to MessageSource, and publication of ApplicationEvent.
ApplicationContext是BeanFactory接口的完整超集,因此它提供了BeanFactory的所有功能。它还提供了集成的生命周期管理、自动注册BeanPostProcessor和BeanFactoryPostProcessor、对MessageSource的便捷访问以及ApplicationEvent的发布。
3. spring-servlet.xml
3、spring-servlet.xml
In Spring, a single front servlet takes the incoming requests and delegates them to appropriate controller methods. The front servlet, based on a Front controller design pattern, handles all the HTTP requests of a particular web application. This front servlet has all the controls over incoming requests.
在Spring中,一个单一的前台Servlet接受传入的请求,并将其委托给适当的控制器方法。基于前台控制器设计模式的前台Servlet处理特定Web应用程序的所有HTTP请求。这个前台Servlet拥有对传入请求的所有控制权。
Similarly, spring-servlet acts as a front controller servlet and provides a single entry point. It takes the incoming URI. Behind the scenes, it uses HandlerMapping implementation to define a mapping between requests and handler objects.
同样,spring-servlet作为一个前台控制器servlet,提供了一个单一的入口点。它接受传入的URI。在幕后,它使用HandlerMapping实现来定义请求和处理对象之间的映射。
Let’s look at the sample code:
让我们看一下示例代码。
<?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 vs. spring-servlet.xml
4.applicationContext.xml与spring-servlet.xml
Let’s look at the summarize view:
让我们来看看总结的观点。
Feature | applicationContext.xml | spring-servlet.xml |
Framework | It is part of the Spring framework. | It is part of the Spring MVC framework. |
Purpose | A container that defines spring beans. | A front controller that processes the incoming requests. |
Scope | It defines the beans that are shared among all servlets. | It defines servlet-specific beans only. |
Manages | It manages global things like datasource, and connection factories are defined in it. | Conversely, Only web-related things like controllers and viewresolver will be defined in it. |
References | It cannot access the beans of spring-servlet. | It can access the beans defined in applicationContext. |
Sharing | Properties common to the whole application will go here. | Properties that are specific to one servlet only will go here. |
Scanning | We define the filters to include/exclude packages. | We declare the component scans for controllers. |
Occurrence | It is common to define multiple context files in an application. | Similarly, we can define multiple files in a web application. |
Loading | The file applicationContext.xml is loaded by ContextLoaderListener. | The file spring-servlet.xml is loaded by DispatcherServlet. |
Required | Optional | Mandatory |
5. Conclusion
5.总结
In this tutorial, we learned about the applicationContext and spring-servlet files. Then, we discussed their role and responsibilities in a Spring application. In the end, we looked at the differences between them.
在本教程中,我们了解了applicationContext和spring-servlet文件。然后,我们讨论了它们在Spring应用程序中的作用和职责。最后,我们研究了它们之间的区别。