Spring Boot ApplicationContext

last modified July 6, 2020

Spring Boot ApplicationContext tutorial shows how to use ApplicationContext in a Spring Boot application.

Spring Boot is a popular framework for building enterprise applications in Java, Kotlin, or Groovy.

Spring ApplicationContext

ApplicationContext is a corner stone of a Spring Boot application. It represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.

ApplicationContext provides the following:

ApplicationContext has several implementations. For instance, the ClassPathXmlApplicationContext takes configuration from an XML file on the classpath or AnnotationConfigApplicationContext, which reads configuration using annotations, especially @Configuration.

Getting ApplicationContext

To access the application context, we can autowire the ApplicationContext interface or implement the ApplicationContextAware.

Spring Boot ApplicationContext example

In the following application, we access the application context and use its methods.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───bean
│   │                   MyBean.java
│   └───resources
│           application.yml

This is the project structure of the Spring Boot application.

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'

group 'com.zetcode'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

repositories {

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'

This is the Gradle build file. The spring-boot-starter is the core starter that includes auto-configuration support, logging, and YAML. The application is packaged into a JAR file.

    banner-mode: "off"
    log-startup-info: "false"
      name: "My application"

The application.yml file contains application configuration settings. There are some built-in application properties and we can create our custom ones. The banner-mode property is a Spring built-in property; we turn off the Spring's banner. With the log-startup-info property, we can turn off the startup logging information. The name is a property to set the application name.

package com.zetcode.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

public class MyBean implements ApplicationContextAware {

    private String applicationId;

    public void setApplicationContext(ApplicationContext applicationContext) 
            throws BeansException {

        applicationId = applicationContext.getId();

    public String getApplicationId() {

        return applicationId;

MyBean implements the ApplicationContextAware. Spring Boot injects the application context into the parameter of the setApplicationContext() method, where we get the Id of the Spring application. (The Id here is the name of the application.)

package com.zetcode;

import com.zetcode.bean.MyBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

public class Application implements CommandLineRunner {

    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    public void run(String... args) throws Exception {


        MyBean myBean = applicationContext.getBean(MyBean.class);

In the Application, we create a bean, call its method and set up the Spring Boot application. The CommandLineRunner interface indicates that a bean should run when it is contained within a SpringApplication. It can be used to create command line applications in Spring Boot.

public class Application implements CommandLineRunner {

The @SpringBootApplication annotation enables auto-configuration and component scanning. Spring Boot finds the MyBean annotation and loads it into the application context bean factory.

private ApplicationContext applicationContext;

With the @Autowired annotation we inject our ApplicationContext bean into the field. Now we can access the methods of the context.


We print the application context's display name and Id.

MyBean myBean = applicationContext.getBean(MyBean.class);

We get the MyBean from the bean factory with the getBean() method. Then we call its getApplicationId() method.

$ gradlew -q bootRun
My application
My application

We run the application with gradlew -q bootRun. From the output we can see, that we use the AnnotationConfigApplicationContext.

In this tutorial, we have introduced the Spring Boot ApplicationContext.

List Spring Boot tutorials.