Purpose and Relation of the different contexts in Spring Purpose and Relation of the different contexts in Spring spring spring

Purpose and Relation of the different contexts in Spring


There can be different interpretations, but here is how I see it:

  • Spring Security context, in the meaning of SecurityContext class, holds the authentication, username, authorities (roles) and possibly other information about the current user. The lifespan of such context is the current request, or the security context is persisted between requests using sessions.

  • Spring Context, in the meaning of ApplicationContext class, is the central point of a running Spring application. Its main purpose is to contain the app singleton beans, but it has many other nifty features (several mentioned in another answer). An application can have several ApplicationContexts, but the most common, and simplest case, it only has one. Web applications usually use the WebApplicationContext "extension", which integrates it with the Servlet context.

  • Servlet Context, the the meaning of ServletContext class, is the application-wide context a Servlet webapp has. There is always exactly one per webapp instance in a servlet container like Tomcat. It is not a part of Spring. You seldom use it directly when using Spring. But it is there in the background.

"Context" is quite a generic term, so there may be other contexts too in your environment.


There are many "contexts" and how they are loaded depends how you initialize your application. But normally most applications have a single context that contains all the beans and components that your application requires.

For example, if you loaded an application using a Servlet you could load the entirety with a "root context" that also loads the Servlet Context as it's child.

The hierarchy is like the following "root" context -> any other context. It's that simple.

A described here in the Spring Docs ApplicationContext provides:

  • Bean factory methods for accessing application components. (Fancy talk for things you need in your application without using new() called dependency injection)
  • The ability to load file resources in a generic fashion. (External configuration with properties and profiles)
  • The ability to publish events to registered listeners. (Fancy talk for "when certain things happen do something", read about the observer pattern)
  • The ability to resolve messages to support internationalization. (Files that have all the Strings used in your application so they can be rendered in different languages, read about MessageSource)
  • Inheritance from a parent context. (Like I said before, your application needs "context" and this is where it begins.)

As you are using Spring boot, there is only one context by default: ApplicationContext. This will contain all your things (Beans) and Components you need.

Infact that's the beauty of using Spring boot, minimal configuration and simplified configuration. If you feel you need multiple Contexts I would urge you to use Profiles instead.

Ultimately "Context" is created and defined by your Application, think of it as a configuration (be it XML or Java) that defines your Application. what's "in it" and what "it needs" to work.

If you want to try and understand it more I would urge you to read from the beginning and start with the Introduction to Spring.


In terms of system design, any Context is a collection of common functionality and data, that defined in one place, but has to be used from anywhere in program. And main purpose of Context - reduce the number of dependencies between components of application. Another purpose of Context - simplify access to common functionality and data. Lets consider contexts that you listed in your question.

ServletContext

ServletContext is part of Servlet technology. Many of frameworks are based on this technology (JSF, Spring, Struts and many more). ServletContext contains functionality for communicate with its Servlet container(like Tomcat, Glassfish etc). Basic things that ServletContext provides:

  • get application Initial parameters;
  • get information about request dispatching;
  • add or remove Servlets, Filters and Listeners;
  • get or set container's attributes;
  • log run-time messages;
  • get additional information like Application path, Container version etc.

As you see there is only basic functionality, that can be useful anywhere in application, if you will work with Servlet technology (for example, develop another one mvc-framework based on it).

ApplicationContext

ApplicationContext is main interface of Spring framework application. It has lot of implementations. One of them loads configuration from xml-file (ClassPathXmlApplicationContext), another one loads configuration based on annotations (AnnotationConfigApplicationContext) and so on. Basic things that ApplicationContext provides:

  • bean factory, ability to create new beans based on bean definitions;
  • load resources from different sources (file system, jar files, url etc);
  • ability to publish and listen events;
  • resolve messages from message bundles;

Again, initialized on start, this basic functionality can be useful almost anywhere in your application. That is why it collected to context. Many classes used in background, to provide this abilities, but all your have to know, for use this functionality - just ApplicationContext.

SecurityContext

SecurityContext provides access to authentication data. You can get the name of authenticated user, roles and other details. This information initialized by security module, may be needed in many many places. Сomponents, which use this information know nothing about classes of security module. They just get all needed information from SecurityContext.