What Is a Spring Context?

DZone 's Guide to

What Is a Spring Context?

Spring contexts are your tickets to bringing beans to your Java app. Make sure you know how to set up dependencies and incorporate them into your code.

· Java Zone ·
Free Resource

In this post, we will create a Spring context and get a bean object from it.

What Is a Spring Context?

Spring contexts are also called Spring IoC containers, which are responsible for instantiating, configuring, and assembling beans by reading configuration metadata from XML, Java annotations, and/or Java code in the configuration files.

Technologies Used

  • Spring 4.3.6.RELEASE

  • Maven Compiler 3.6.0

  • Java 1.8

We will first create a simple Maven project. You can select maven-archtype-quickstart as the archetype.

Adding POM Dependencies

We will add spring-framework-bom in our dependency management.


The benefit of adding this is to manage the version of the added Spring dependencies from one place. With this, you can omit mentioning the version number for Spring dependencies.


Now, we will create a class, GreetingService, which is eligible to be registered as a bean in a Spring context.

public class GreetingService {
    private static final Logger logger = Logger.getLogger(GreetingService.class.getName());

    public GreetingService() {


    public void greet() {
        logger.info("Gaurav Bytes welcomes you for your first tutorial on Spring!!!");

@Service, at the class level, means that this is a service and is eligible to be registered as a bean in the Spring context.

Instantiating a Container

Now, we will create the object of a Spring context. We are using AnnotationConfigApplicationContext as a Spring container. Also, there are other Spring containers like ClassPathXmlApplicationContext, GenericGroovyApplicationContext, etc. which we will discuss in future posts.

ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(

As you see at the time of object construction for AnnotationConfigApplicationContext, I am passing one string parameter. This parameter (of the varags type) is the basePackages , which the Spring context will scan for bean registration.

Now, we will get a bean object by calling getBean() on the Spring context.

GreetingService greetingService = context.getBean(GreetingService.class);

At last, we are closing the Spring container by calling close().


It is important to close the spring context (container) after use. By closing it, we ensure that it will release all the resources and locks that its implementation might hold and will also destroy all the cached singleton beans.

We have also included maven-compiler-plugin in pom.xml to compile the Java sources with the configured Java version (in our case, it is Java 1.8).

You can also find the example code on GitHub.

ioc containers, java, spring beans, spring context, tutorial

Published at DZone with permission of Gaurav Rai Mazra , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}