Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose and Relation of the different contexts in Spring

Tags:

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?)

like image 376
Harshana Avatar asked Mar 03 '16 12:03

Harshana


People also ask

How many types of context are there in Spring?

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.

What is the use of context in Spring?

Furthermore, it provides more enterprise-specific functionalities. The important features of ApplicationContext are resolving messages, supporting internationalization, publishing events, and application-layer specific contexts. This is why we use it as the default Spring container.

What is the purpose of Spring Java?

The Spring Framework (Spring) is an open-source application framework that provides infrastructure support for developing Java applications. One of the most popular Java Enterprise Edition (Java EE) frameworks, Spring helps developers create high performing applications using plain old Java objects (POJOs).

Can we have multiple context in Spring?

It's possible to create separate contexts and organize them in a hierarchy in Spring Boot. A context hierarchy can be defined in different ways in Spring Boot application. In this article, we'll look at how we can create multiple contexts using the fluent builder API.


1 Answers

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.

like image 157
holmis83 Avatar answered Oct 01 '22 16:10

holmis83