Why this matters
- Every
@Service,@Repository, and@Controllerin BookStore is a bean managed by the container — not created withnew. - Misunderstanding bean lifecycle leads to
NullPointerExceptionfrom field injection or duplicate instances from manual construction. - Container internals are a core Spring interview topic; explaining bean creation order demonstrates real framework knowledge.
Bean = container-managed object
L1 · FresherSpring constructs BookService, injects BookRepository, and stores the instance in the ApplicationContext. You ask for types with @Autowired constructor parameters instead of calling new.
ApplicationContext responsibilities
L2 · IntermediateThe context reads bean definitions (annotations, @Bean methods), resolves dependency order, and publishes lifecycle events. getBean is for frameworks and tests — prefer injection in app code.
Circular dependencies
L3 · AdvancedConstructor cycles fail fast at startup with BeanCurrentlyInCreationException. Fix by redesigning boundaries, @Lazy on one side, or setter injection only when justified.
BeanFactory vs ApplicationContext
L4 · ExpertBeanFactory is the low-level registry; ApplicationContext adds internationalization, events, and resource loading. Boot uses an application context implementation specialized for servlet and reactive stacks.
What is a bean?
A bean is any object whose lifecycle Spring controls. For BookStore:
// learn:fragment
@Service
public class BookService {
private final BookRepository repository;
public BookService(BookRepository repository) {
this.repository = repository;
}
}
Spring instantiates BookService once, injects the BookRepository bean, and reuses that same instance for every HTTP request.
Bean registration methods
- Component scanning —
@Service,@Component,@Repository,@Controlleron a class. @Beanmethods — Explicit factory methods inside@Configurationclasses.- Auto-configuration — Boot creates beans like
DataSourcewhen starters are on the classpath.
Arrows show @Autowired wiring — container injects at startup
ApplicationContext vs BeanFactory
BeanFactory is the low-level interface for bean retrieval. ApplicationContext extends it with:
ApplicationContext extras
- Automatic
BeanPostProcessorregistration - Message source internationalisation
- Event publication (
ApplicationEventPublisher) - Resource loading from classpath
Boot uses AnnotationConfigServletWebServerApplicationContext — you never instantiate it directly.
Bean definition and creation
At startup the container processes bean definitions — metadata describing how to build each bean:
Bean creation pipeline
- Scan — Find
@Componentclasses incom.example.bookstore.** - Parse — Read constructor parameters as injection points
- Sort — Order beans to satisfy dependencies (topological sort)
- Instantiate — Call constructors, apply
@Autowiredwhere needed - Initialise — Run
@PostConstruct,InitializingBean.afterPropertiesSet() - Register — Store in the singleton cache
If BookService depends on BookRepository which depends on DataSource, the container creates them in that order automatically.
Retrieving beans programmatically
Rarely needed in application code, but useful in tests:
// learn:fragment
@SpringBootTest
class BeanInspectionTest {
@Autowired
private ApplicationContext context;
@Test
void bookServiceIsSingleton() {
BookService a = context.getBean(BookService.class);
BookService b = context.getBean(BookService.class);
assertSame(a, b);
}
}
Both references point to the same object — default scope is singleton.
When the container fails
Circular dependencies crash startup:
// learn:fragment
@Service
public class OrderService {
public OrderService(BookService bookService) { ... }
}
@Service
public class BookService {
public BookService(OrderService orderService) { ... } // cycle!
}
Spring detects the cycle and throws BeanCurrentlyInCreationException. Fix by restructuring dependencies or using @Lazy on one side.
Bean naming
Default bean name is the decapitalised class name: bookService for BookService. Override with @Service("catalogService") when multiple implementations exist.
// learn:fragment
public interface BookRepository { ... }
@Repository
public class JpaBookRepository implements BookRepository { ... }
Injection by type works when only one implementation exists. Multiple implementations require @Qualifier or @Primary.
Context refresh and shutdown
In production the context starts once and runs until shutdown. @PreDestroy methods clean up resources:
// learn:fragment
@Service
public class CacheWarmupService {
@PreDestroy
public void shutdown() {
// flush pending cache writes
}
}
Boot registers a shutdown hook so SIGTERM triggers graceful bean destruction.
Bean lifecycle
For a singleton BookService, the container walks a fixed sequence. Remembering the order explains why @Autowired fields are null inside the constructor and why an AOP proxy exists only after initialization.
From definition to destruction
- Load the definition — Component scan or a
@Beanmethod records the class, scope, and dependencies. Nothing is constructed yet. - Instantiate — The container calls the constructor and supplies constructor arguments. This is the only moment
finaldependencies can be set. - Populate — Setter and field injection run. Prefer not to need this step.
- Aware callbacks —
BeanNameAware,BeanFactoryAware, andApplicationContextAwareif you implemented them. - Before-init post-processors —
BeanPostProcessor.postProcessBeforeInitialization. - Initialize —
@PostConstruct, thenInitializingBean.afterPropertiesSet. - After-init post-processors —
postProcessAfterInitialization. Spring AOP wraps the bean in a proxy here. Callers must use that proxy, not the raw instance. - Ready — The singleton is stored in the context and injected into other beans.
- Destroy — On shutdown,
@PreDestroythenDisposableBean.destroy. Prototype beans skip this; you destroy those yourself.
// learn:fragment
@Service
public class BookService {
private final BookRepository books;
public BookService(BookRepository books) {
this.books = books; // step 2 — repository is already created
}
@PostConstruct
void ready() {
// step 6 — safe to use dependencies
}
@PreDestroy
void shutdown() {
// step 9 — context is closing
}
}
Inversion of control is this sequence applied to every bean: your classes declare what they need, and the container decides when they are created, wired, and destroyed.
Quick recall
Everything you need if you only revisit this box.
- Beans are objects whose lifecycle and dependencies the ApplicationContext manages.
- Default scope is singleton — one instance per application context.
- Registration via component scanning (
@Service) or explicit@Beanmethods. - The container topologically sorts dependencies and fails fast on circular references.
- Never use
newfor Spring-managed services; injection provides wired instances. ApplicationContextextendsBeanFactorywith events, resources, and post-processors.
Test yourself
Answer these before moving on — recall is what makes it stick.