What Is a Spring Bean?
Fast recall
Quick revision
Core answer
Fundamental understanding of what distinguishes a Spring-managed bean from a regular Java object in the same application
Assuming every object you create with new keyword in a Spring app is automatically a Spring bean and gets dependency injection
Clearly distinguish that beans have a managed lifecycle and can be injected, while regular objects you create with new are invisible to Spring and don't get any container features
Interview-ready guide
Interview answer
@Service on PricingService makes it a component candidate only when its package is scanned. A @Bean method can register a third-party client. A FactoryBean can create another object, and an existing singleton can be registered programmatically. The defining fact is container registration and management, not one annotation or the claim that Spring always called the constructor itself.
What Is a Spring Bean
Direct Answer
A Spring bean is an object registered with and obtained from a Spring BeanFactory/ApplicationContext. The container applies configured creation, dependency injection, post-processing, scope, and lifecycle behavior. Not every bean is physically created by Spring, and destruction is not complete for every scope: factory methods can return existing objects, and prototype destruction is the caller's responsibility.
Code Example
// Method 1: Stereotype annotations (auto-detected via component scanning)
@Service // Makes this class a Spring bean
public class OrderService {
private final OrderRepository repository;
public OrderService(OrderRepository repository) { // Spring injects repository
this.repository = repository;
}
public void placeOrder(Order order) {
repository.save(order);
}
}
@Repository // Also a Spring bean, specialized for data access
public class OrderRepository {
// Spring manages this as a singleton bean
}
// Method 2: Explicit @Bean declaration in configuration class
@Configuration
public class AppConfig {
@Bean // Spring calls this method and manages the returned object
public ObjectMapper jsonMapper() {
var mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper; // This ObjectMapper instance becomes a Spring bean
}
@Bean
public RestTemplate restTemplate(ObjectMapper mapper) { // Spring injects the bean above
return new RestTemplate();
}
}
// NOT a Spring bean (plain Java object)
public class OrderValidator {
public boolean validate(Order order) {
return order.total() > 0; // Spring doesn't manage this, no DI happens
}
}
Common Mistakes & Pitfalls
- Mixing new with DI: Freshers annotate a class with @Service but then create instances with new OrderService() in other beans, bypassing Spring's container. That new instance isn't a bean — it won't get dependencies injected, won't be transactional, and lives outside Spring's control. Always let Spring create beans by injecting them.
- Forgetting component scanning: Annotating a class with @Component but placing it outside the base package Spring scans (usually the package with @SpringBootApplication), so Spring never finds it. Check your component scan configuration or move the class under the scanned package.
- Expecting every object to be a bean: Treating JPA entities or DTOs like Order or CustomerDTO as Spring beans and trying to inject dependencies into them. Entities are managed by JPA, not Spring — they're data objects, not service layer components. If an entity needs business logic, put that logic in a separate @Service bean.
Check your understanding
Answer these without looking back at the article.
- Is every Java object in a Spring app a Spring bean?
- What is the difference between a Spring bean and a POJO?
- How does Spring know which classes to create as beans?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.