Spring Bean Definition Explained
Fast recall
Quick revision
Core answer
Understanding that Spring separates bean metadata (definition) from the actual object instance
Thinking bean definition and bean instance are the same thing, when definition is the recipe and instance is the cooked meal
Explain that Spring reads annotations or XML to create BeanDefinition objects before any beans are instantiated, showing you understand the two-phase process
Interview-ready guide
Interview answer
Spring separates the recipe from the object. Component scanning and @Bean methods register bean definitions. During context refresh or later lookup, the BeanFactory uses those definitions to choose a constructor or factory method, resolve dependencies, apply post-processors, and create an instance. A singleton definition normally leads to one shared instance in that container, while a prototype definition can produce multiple objects. This distinction explains how Spring can inspect, modify, or replace definitions before any application bean exists.
Spring Bean Definition Explained
Direct Answer
A Spring bean definition is metadata that tells the container how to create and manage an object. It can describe the bean class or factory method, scope, constructor arguments, property values, qualifiers, lazy behavior, and lifecycle methods. The BeanFactory stores definitions; the actual bean instance may be created later according to its scope and lazy settings.
Code Example
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
@Service
class OrderService {
private final PaymentGateway gateway;
public OrderService(PaymentGateway gateway) { this.gateway = gateway; }
}
@Service
class PaymentGateway {}
public class BeanDefinitionDemo {
public static void main(String[] args) {
var context = new AnnotationConfigApplicationContext("com.example");
// Bean definition exists before bean instance is created
var definition = context.getBeanDefinition("orderService");
System.out.println("Bean class: " + definition.getBeanClassName());
System.out.println("Scope: " + definition.getScope()); // singleton
System.out.println("Lazy init: " + definition.isLazyInit()); // false
// Now get the actual instance (created from the definition)
var service = context.getBean(OrderService.class);
}
}
Common Mistakes & Pitfalls
- Confusing definition with instance: Freshers often think getBeanDefinition and getBean return the same thing. The definition is metadata; the instance is the living object. Modifying a definition after startup doesn't change existing singletons.
- Expecting definitions to exist for manual new calls: If you write new OrderService(gateway) instead of injecting it, Spring has no bean definition for that instance—it won't be managed, proxied, or eligible for @Transactional.
- Not realizing definitions are created at startup: All definitions are registered during application context refresh, before any bean is instantiated. If a class isn't scanned (wrong package) or lacks @Component, no definition exists and you'll get NoSuchBeanDefinitionException at runtime.
Check your understanding
Answer these without looking back at the article.
- What is the difference between a bean definition and a bean instance?
- What information does a bean definition contain?
- How does Spring store bean definitions internally?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.