Spring Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> LEARN SPRING

1. Introduction

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.

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

In this article, we'll look at the differences between the applicationContext and spring-servlet.

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.

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.

<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.

In short, applicationContext is the central interface in Spring. It provides configuration information for an application.

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.

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.

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.

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

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

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.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments