Skip to content
Ameen Abozaid
JNR-010

Spring container

  • 6 Lessons
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • ApplicationContext
  • bean scopes
  • component scanning
  • stereotypes

Learn first

2 h of practice (estimate)

Learning outcomes

  1. Explain the ApplicationContext and how beans are discovered by component scanning.
  2. Choose between @Component, @Service, @Repository, @Controller, and @Bean methods in @Configuration classes.
  3. Choose singleton or prototype scope and explain the thread-safety implication of singletons.
  4. Diagnose a bean that was not found because of package layout.

Lessons

  1. 01The ApplicationContext
  2. 02Component scanning and stereotypes
  3. 03@Configuration and @Bean
  4. 04Bean scopes
  5. 05Stateless singletons and thread safety
  6. 06Demo: injecting a Clock to test time-dependent logic

Lab and exercises

Lab

JNR-010-LAB1 · Third-party beans

Outcomes: 1–4 · Time: ~2 h

  1. Create a @Configuration class with @Bean methods for a Clock and a currency client (a simple third-party-style class).
  2. Inject Clock into the promotion and order services; replace every now() call.
  3. Write a test using Clock.fixed(...) for a time-dependent rule.
  4. Move the main class to a sub-package, observe the missing-bean failure, then fix the layout.

Acceptance: no static now() calls in services; time-dependent tests are deterministic; the layout failure is explained.

Exercises (assessment: "why is this bean missing?" debugging exercise)

IDOutcomeTask
JNR-010-EX011Print the number of beans and the names starting with product from the ApplicationContext.
JNR-010-EX022For each class choose a stereotype or @Bean: ProductService, ProductRepository, ProductController, a third-party HttpClient.
JNR-010-EX032Write a @Configuration class exposing a Clock bean and inject it into a service.
JNR-010-EX043Add a private int counter to a singleton, hit the endpoint twice, and explain the result; then remove the state.
JNR-010-EX053Show that two injections of a prototype bean differ and two of a singleton do not (compare references).
JNR-010-EX064Given a project whose main class is in com.shop.app and a service in com.shop.service, explain the startup failure and give two fixes.
Show solutions
  • EX01 ctx.getBeanDefinitionCount() and Arrays.stream(ctx.getBeanDefinitionNames()).filter(n -> n.startsWith("product")).
  • EX02 @Service, @Repository, @RestController, @Bean in a @Configuration.
  • EX03 @Bean Clock clock(){ return Clock.systemUTC(); } and a constructor parameter Clock clock.
  • EX04 The counter grows across requests because one instance serves all; keep services stateless.
  • EX05 a == b is true for singletons and false for prototypes.
  • EX06 The service package is outside com.shop.app, so it is not scanned: NoSuchBeanDefinitionException. Fix: move the main class to com.shop, or set scanBasePackages (the first is preferred).

Quiz

Outcomes: O1 ApplicationContext and scanning · O2 stereotypes and @Bean · O3 scope and thread safety · O4 missing-bean diagnosis.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1The ApplicationContext is:O1
  2. 2Component scanning finds beans in:O1
  3. 3Best annotation for business logic:O2
  4. 4To register a third-party class you cannot edit:O2
  5. 5@Repository additionally:O2
  6. 6The default scope is:O3
  7. 7Why must singleton services be stateless?O3
  8. 8A prototype bean is requested:O3
  9. 9A service in com.shop.service is missing and the main class is in com.shop.app. Likely cause:O4
  10. 10The simplest layout fix:O4

Practical part

Six exercises JNR-010-EX01 to EX06 plus the lab. The assessment is the "why is this bean missing?" exercise (EX06). Pass: quiz at least 8/10, EX06 diagnosed with a correct fix, lab acceptance met.

Common mistakes

Mutable state in singleton services; main class in the wrong package.

Assessment

"why is this bean missing?" debugging exercise.

Career relevance

Concurrency awareness (MID-004) starts with stateless singletons.

Type to search levels, phases, modules and lessons.