liveBook · Manning

Application context

As explained in Spring in Action, Sixth Edition

A Spring container that creates, configures, and manages an application's beans, dependencies, and runtime configuration.

A Spring application context is the container that creates and manages an application’s components, called beans. It also connects beans that depend on each other. Instead of making a product service create its own inventory service, the context can supply that dependency when it creates the product service. This is dependency injection. section 1.1

@Configuration
public class ServiceConfiguration {
  @Bean
  public InventoryService inventoryService() {
    return new InventoryService();
  }
 
  @Bean
  public ProductService productService() {
    return new ProductService(inventoryService());
  }
}

In this Java configuration example, @Bean methods declare the two services. The product service receives the inventory service through its constructor. section 1.1

You do not have to declare every bean yourself. Beans can be declared with Java or XML, found through component scanning, or configured automatically by Spring Boot. With web and Thymeleaf starters present, Boot configures beans for Spring MVC, an embedded Tomcat server, and a Thymeleaf view resolver. section 1.5 section 1.3.6

Listing 1.2 The Taco Cloud bootstrap class
package tacos;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication                                        #1
public class TacoCloudApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(TacoCloudApplication.class, args);  #2
  }
 
}

The bootstrap class starts the application with SpringApplication.run. It is a practical starting point for seeing how a Spring Boot application launches. section 1.2.2

Listing 1.3 A baseline application test
package tacos;
 
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest                            #1
public class TacoCloudApplicationTests {
 
  @Test                                    #2
  public void contextLoads() {
  }
 
}

A minimal @SpringBootTest with an empty contextLoads test checks that the application context starts successfully. section 1.2.2

During development, Spring Boot DevTools can restart the application context after project code or property files change. A dependency change still needs a full restart because DevTools does not automatically reload dependency libraries. section 1.3.5

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines the container, illustrates constructor injection and Java bean declarations, and describes autoconfiguration without detailing container internals.
Examples
Uses an inventory-service dependency, Java bean configuration, a Spring Boot bootstrap class, and a context-loading test.
What it assumes
Starts with ordinary application components and defines beans and dependency injection before using a generated Spring project.
What you can do after
Apply it via Spring Boot to register beans, start an application, and check that its context loads.
How it connects
Connects beans, dependency injection, component scanning, and Spring Boot autoconfiguration.

Also explained in

Compare how 2 books explain application context →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.