I am using a spring boot project. I want to understand the purpose and relation of different contexts?
For instance, Spring Security context, Spring Context, Servlet Context, etc. (Are there any more contexts?)
I am using a spring boot project. I want to understand the purpose and relation of different contexts?
For instance, Spring Security context, Spring Context, Servlet Context, etc. (Are there any more contexts?)
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:
new()
called dependency injection)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:
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:
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
.