@Bean Annotation in Spring — Usage and Purpose
Fast recall
Quick revision
Core answer
Knowledge of how to register beans when you don't control the source code
Assuming every @Bean method has full @Configuration proxy semantics; @Bean can also be declared in lite configuration such as a @Component, where direct inter-method calls are ordinary Java calls
Give real-world examples: registering Jackson's ObjectMapper, Spring's RestTemplate, or a DataSource from HikariCP
Interview-ready guide
Interview answer
A configuration method can accept PaymentProperties and return an HttpClient configured with its timeout. Spring registers that returned client, injects it elsewhere, and manages lifecycle callbacks when applicable. The method can also create two clients with different names or qualifiers. This is more explicit than component scanning and works for classes from libraries that cannot carry Spring annotations.
@Bean Annotation in Spring — Usage and Purpose
Direct Answer
@Bean marks a factory method whose returned object is registered and managed by Spring. It is commonly declared in a @Configuration class, but @Bean methods can also appear in other component classes in lite mode. Use it for third-party objects, customized construction, or explicit named/qualified instances—not only when @Component is impossible.
Code Example
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
class AppConfig {
@Bean
public ObjectMapper objectMapper() {
var mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule()); // Java 8 date/time support
return mapper;
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
// Inject another bean as parameter
@Bean
public PaymentClient paymentClient(RestTemplate restTemplate) {
return new PaymentClient(restTemplate, "https://api.payment.com");
}
}
Common Mistakes & Pitfalls
- Putting @Bean outside @Configuration: Freshers annotate a regular @Service class method with @Bean. Spring ignores it unless the class is @Configuration. The method won't be called and no bean is registered.
- Calling @Bean methods directly: Calling objectMapper() from another method in the same class. In a @Configuration class, Spring intercepts these calls and returns the singleton. But freshers sometimes manually call the method thinking they're creating a new instance, leading to confusion about singleton behavior.
- Not returning the object: Writing a @Bean method that returns void or null. Spring stores null as the bean instance, causing NullPointerException when injected. Always return the configured object.
- Forgetting method parameters inject dependencies: Writing complex constructor logic inside the method instead of accepting dependencies as parameters. Spring can inject beans into @Bean method parameters automatically.
Check your understanding
Answer these without looking back at the article.
- What is the bean name when you use @Bean?
- Can @Bean methods have parameters?
- What is @Configuration and why is it needed?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.