Interview Practice

Chapter 1 of 5

Spring container, injection and auto-configuration

Understand how an application is assembled before using annotations.
Read aloud is unavailable in this browser. Reading preferences still work.

The container creates an object graph

Spring manages configured objects called beans and supplies their dependencies. Constructor injection expresses mandatory collaborators, supports final fields and makes unit construction possible without a container. Component scanning discovers eligible classes within configured packages; explicit configuration can register others. An annotation on a class outside the scanned/imported configuration does not automatically make it a managed object.

Boot supplies conditional defaults

Spring Boot uses starters, auto-configuration and conventions to reduce setup. Conditions can depend on classpath, application type, properties and existing beans. A user bean can make a conditional default back off where the relevant condition supports that behavior. Inspect the condition evaluation report when configuration surprises you. Spring Boot versions differ in modules and packages, so examples should be read against the application’s selected version.

Scope is not synchronization

A singleton is normally one bean instance per bean definition per container. Mutable request state in a singleton is shared, so storing a current user or order there can mix requests. Prefer stateless services and put request-specific data in parameters or an appropriate scoped object. Constructor dependency cycles often indicate responsibilities that should be separated rather than a need for a clever injection workaround.

Worked Java example

Illustrative snippet: imports, surrounding types or collaborators may be omitted. Read the assumptions before adapting it.

@Service
class OrderService {
    private final OrderRepository orders;
    OrderService(OrderRepository orders) {
        this.orders = orders;
    }
    Order find(long id) {
        return orders.findById(id).orElseThrow();
    }
}

Walkthrough

This illustrative service requires a repository at construction. Spring can supply that dependency when both beans are registered; a plain unit test can pass a fake directly. The example omits domain exception translation and assumes the repository contract shown. The final field fixes the reference, not the internal mutability or thread safety of the repository implementation.

Practice it yourself

A service constructor needs twelve collaborators. What would you review before changing the injection style?

Check your reasoning

Review cohesion and the use cases the service owns. Extract independently meaningful responsibilities where appropriate. Field injection would hide the dependency count rather than solve the coupling. Do not split mechanically into meaningless forwarding classes.

Recall checklist

  • Explain the main invariant or contract without looking.
  • Trace the example and test an edge case.
  • State time, space, input variables and assumptions where relevant.
  • Explain one alternative and when you would choose it.

Read further